-
Notifications
You must be signed in to change notification settings - Fork 47
/
kruskal algorithm
67 lines (58 loc) Β· 1.17 KB
/
kruskal algorithm
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
#include<bits/stdc++.h>
using namespace std;
struct edge{
int start,end,weight;
bool operator<(edge const& other) {
return weight < other.weight;
}
};
int n;
vector<edge> edges;
vector<edge> result;
void make_set(int v,vector<int>& parent) {
parent[v] = v;
}
int find_parent(int v,vector<int>& parent){
if (v == parent[v]){
return v;
}
return find_parent(parent[v],parent);
}
void union_find(int a,int b,vector<int>& parent){
a = find_parent(a,parent);
b = find_parent(b,parent);
if(a != b){
parent[b] = a;
}
}
void kruskal(){
//make_parent();
vector<int> parent(n);
for (int i = 1; i <= n; i++){
make_set(i,parent);
}
int cost = 0,count = 0;
sort( edges.begin(),edges.end() );
for(auto e : edges){
if( find_parent(e.start,parent) != find_parent(e.end,parent) ){
cost += e.weight;
result.push_back(e);
union_find(e.start, e.end,parent);
count++;
}
if(count == n-1){
break;
}
}
cout << cost << endl;
}
int main(){
int e;
cin >> n >> e;
for(int i=1;i<=e ;i++){
int start , end , weight;
cin >> start >> end >> weight;
edges.push_back({start,end,weight});
}
kruskal();
}