-
Notifications
You must be signed in to change notification settings - Fork 0
/
Krushkal Algorithm Practice.cpp
67 lines (55 loc) · 1.08 KB
/
Krushkal Algorithm Practice.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
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
using namespace std;
struct data{
int u, v, w;
bool operator < (const data& p) const
{
return w<p.w;
}
};
vector<data>graph;
int parent[100];
void init(int n)
{
for(int i=1; i<=n; i++)
parent[i] = i;
}
int findnode(int n)
{
(parent[n]!=n)?findnode(parent[n]):parent[n];
}
int MST(int n)
{
init(n);
sort(graph.begin(),graph.end());
int szg = graph.size();
int cost = 0, cnt=0;
for(int i=0; i<szg; i++)
{
int u = findnode(graph[i].u);
int v = findnode(graph[i].v);
if(u==v) cout<<graph[i].u<<' '<<graph[i].v<<" Cycle"<<endl;
if(u!=v)
{
parent[v] = u;
cnt++;
cost = cost + graph[i].w;
if(cnt==n-1) break;
}
}
return cost;
}
int main()
{
int nodes, edges, u, v, w;
scanf("%d %d", &nodes, &edges);
for(int i=1; i<=edges; i++)
{
scanf("%d %d %d", &u, &v, &w);
data var;
var.u = u; var.v = v; var.w = w;
graph.push_back(var);
}
printf("%d\n",MST(nodes));
return 0;
}