-
Notifications
You must be signed in to change notification settings - Fork 10
/
kruskal_mst.cpp
58 lines (51 loc) · 1.05 KB
/
kruskal_mst.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX 100000
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
vector<iii> edges;
struct NODE
{
NODE *h;
} head[MAX];
bool comp(iii a, iii b)
{
return a.second < b.second;
}
int main()
{
ios_base::sync_with_stdio(false);
int n, a, b, c, m;
cin>>n;
cin>>m;
for(int i = 0; i < m; i++)
{
cin>>a;
cin>>b;
cin>>c;
head[a].h = &head[a];
head[b].h = &head[b];
edges.push_back(iii(ii(a, b), c));
}
sort(edges.begin(), edges.end(), comp);
int count = 0, total = 0, added = 0;
while(added < n-1 && count < edges.size())
{
iii temp = edges[count];
ii edge = temp.first;
int u = edge.first, v = edge.second;
int cost = temp.second;
count++;
while(head[u].h->h != head[u].h) head[u].h = head[u].h->h;
while(head[v].h->h != head[v].h) head[v].h = head[v].h->h;
if(head[u].h == head[v].h)
continue;
head[v].h->h = head[u].h;
added++;
total += cost;
}
cout<<total<<"\n";
return 0;
}