-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskal Algotihm.cpp
96 lines (79 loc) · 1.48 KB
/
Kruskal Algotihm.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <iostream>
#include <set>
#define MAXN 100;
#define F first
#define S second
using namespace std;
int P[1000], rank[1000];
typedef pair<int,pair<int, int> > arista;
int create_set(int x)
{
P[x] = x;
rank[x] = 0;
}
int find_set(int x)
{
if( x != P[x] )P[x] = find_set( P[x] );
return P[x];
}
int merge_set(int x, int y)
{
int Px = find_set( x );
int Py = find_set( y );
if( rank[Px] > rank[Py] ) {
P[Py] = Px;
rank[Px] += rank[Py];
}
else{
P[Px] = Py;
rank[Py] += rank[Px];
}
}
//bool mycomp(arista a, arista b)
//{
// return a < b;
//}
priority_queue<arista, vector<arista> ,greater<arista> > Q;
int main()
{
int n, e, ini, fin, cost;
cin>> n >> e;
for(int i = 0; i < e;i++){
cin>> ini>>fin>> cost;
arista A = make_pair( cost, make_pair(ini, fin) );
Q.push( A );
}
for(int i = 1;i <= n;i++){
create_set(i);
}
int sol = 0;
while(!Q.empty()){
arista A = Q.top();
Q.pop();
cout<<A.S.F<<" "<<A.S.S<<endl;
if( find_set(A.S.F) != find_set(A.S.S) ){
merge_set( A.S.S, A.S.F);
cout<< A.S.F<<" "<<A.S.S<<endl;
system("pause");
sol += A.F;
}
}
cout<<"Costo del Arbol de costo minimo:"<<sol<<endl;
return 0;
}
/***
5 10
1 2 2
2 5 3
2 3 5
3 4 3
4 6 2
5 6 6
5 3 7
1 3 1
1 4 6
3 6 4
***/