-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkruskal.cpp
More file actions
69 lines (55 loc) · 1.25 KB
/
Copy pathkruskal.cpp
File metadata and controls
69 lines (55 loc) · 1.25 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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
const int N = 100005;
int id[N], sz[N];
vector<ppi> g;
void init(int n){
for(int i = 0; i < n; i++){
id[i] = i; sz[i] = 1;
}
}
int Find(int i){
if(i == id[i]) return i;
return id[i] = Find(id[i]);
}
void Union(int vv, int ww){
vv = Find(vv); ww = Find(ww);
if(vv == ww) return;
if(sz[vv] > sz[ww]) swap(vv, ww);
id[vv] = ww;
if(sz[vv] == sz[ww]) sz[ww]++;
}
bool edgecmp(ppi a, ppi b){
return a.second < b.second;
}
long long kruskal(int nn, int ee){
long long cst = 0LL;
init(nn);
for(int i = 0; i < ee; i++){
int uu = g[i].first.first;
int vv = g[i].first.second;
if(Find(uu) != Find(vv)){
Union(uu, vv);
cst += g[i].second;
}
}
return cst;
}
int main()
{
int nn, mm;
while(scanf("%d %d", &nn, &mm), nn){
g.resize(mm);
for(int xx, yy, cst, i = 0; i < mm; i++){
scanf("%d %d %d", &xx, &yy, &cst);
g[i] = ppi(pii(xx, yy), cst); // cuidado quando vertices forem de 1 a nn - > pii(xx - 1, yy - 1)
}
sort(g.begin(), g.end(), edgecmp);
long long custo = kruskal(nn, mm);
printf("%Ld\n", custo);
g.clear();
}
return 0;
}