-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal Algorithm.cpp
More file actions
62 lines (55 loc) · 1.29 KB
/
Kruskal Algorithm.cpp
File metadata and controls
62 lines (55 loc) · 1.29 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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
using namespace std;
#define INF 987654321
#define forn(i,n) for(int i=0;i<(int)n;++i)
#define formy(i,r,l) for(int i=r;i<=(int)l;++i)
#define pii pair<int,int>
#define sz(a) int((a).size())
#define mp make_pair
#define LL long long
#define len(a) int((a).length())
#define pb push_back
#define LL long long
int link[100]; // Node Constructed with 100 Nodes
int find(int a){
while(a!=link[a]) a=link[a];
return a;
}
bool same(int a, int b){
a=find(a);
b=find(b);
link[b]=a;
}
void Union(int a,int b){
a=find(a);
b=find(b);
link[b]=a;
}
int main(){
// pair constructed with start point, end point, weight
vector<pair<int,pii>> edge;
int minSpanningWeight=0;
forn(i,100) link[i]=i;
sort(edge.begin(),edge.end()); // sort
forn(i,sz(edge)){
int start=edge[i].first;
int destination=edge[i].second.first;
int w=edge[i].second.second;
// Check the Cycle
if(!same(start,destination)){
Union(start,destination);
minSpanningWeight+=w;
}
}
cout<< minSpanningWeight<< endl;
return 0;
}