-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.cpp
More file actions
158 lines (123 loc) · 2.65 KB
/
Copy pathbinary.cpp
File metadata and controls
158 lines (123 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> >v[100005];
int done[100005];
struct heap{
int val;
int to;
int from;
}h[2000005];
int sz=0;
void print(){
printf("\nPrinting\n");
for(int i=1;i<=sz;i++){
printf("%d\n",h[i].val);
}
printf("\n");
}
void heapify(int node){
if(node==1){
return ;
}
int par=node/2;
if(h[node].val<h[par].val){
swap(h[node],h[par]);
heapify(par);
}
}
void insert(int v,int t,int f){
sz++;
h[sz].val=v;
h[sz].to=t;
h[sz].from=f;
heapify(sz);
//print();
}
pair<int,pair<int,int> >getmin(){
return make_pair(h[1].val,make_pair(h[1].to,h[1].from));
}
void recurse(int node){
int l=2*node,r=l+1;
if(h[l].val==-1){
return ;
}
else if(h[r].val==-1){
if(h[node].val>h[l].val){
swap(h[node],h[l]);
return ;
}
return ;
}
if(h[node].val <= h[l].val && h[node].val<=h[r].val)
return ;
if(h[l].val<=h[r].val){
swap(h[node],h[l]);
recurse(l);
}
else{
swap(h[node],h[r]);
recurse(r);
}
}
void deletemin(){
swap(h[sz],h[1]);
h[sz].val=-1;
sz--;
//print();
recurse(1);
//print();
}
int main(){
int n,e;
scanf("%d %d",&n,&e);
for (int i=0;i<e;i++){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
v[a].push_back(make_pair(b,c));
v[b].push_back(make_pair(a,c));
}
for (int i=0;i<=4*e;i++){
h[i].val=-1;
}
for (int i=0;i<v[1].size();i++){
insert(v[1][i].second,1,v[1][i].first);
}
/*for (int i=0;i<n;i++){
int choice;
scanf("%d",&choice);
if(choice==1){
int x;
scanf("%d",&x);
insert(x,1,2);
}
else{
pair<int,pair<int,int> >p=getmin();
printf("min is %d\n",p.first);
deletemin();
}
}*/
clock_t start = clock();
int cnt=1;
long long ans=0;
done[1]=1;
while(cnt<n){
pair<int,pair<int,int> >p=getmin();
while(done[p.second.second]){
deletemin();
p=getmin();
}
ans+=p.first;
int node=p.second.second;
done[node]=1;
for (int i=0;i<v[node].size();i++){
if(!done[v[node][i].first]){
insert(v[node][i].second,node,v[node][i].first);
}
}
cnt++;
}
clock_t stop = clock();
printf("COST = %lld\n",ans);
double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
printf("Time elapsed in ms: %f\n", elapsed);
}