-
Notifications
You must be signed in to change notification settings - Fork 1
/
kruskal.cpp
89 lines (67 loc) · 1.02 KB
/
kruskal.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
#include <iostream>
#include <fstream>
using namespace std;
int m, n, c=0, a[100][100];
ifstream f("graf.txt");
struct muchie{
int x, y, c;
} v[100];
int l[100];
void citire(){
int x, y;
for(int i=0; i<m; i++){
f>>v[i].x>>v[i].y>>v[i].c;
}
}
void ordonare_muchii(){
muchie t;
for(int i=0; i<m-1; i++){
for(int j=i+1; j<m; j++){
if(v[i].c > v[j].c){
t = v[i];
v[i] = v[j];
v[j] = t;
}
}
}
}
void kruskal(){
int i, ct=0, comp1, comp2;
for(i=0; i<n; i++){
l[i] = i;
}
i=-1;
while(ct<n-1){
i++;
comp1 = l[v[i].x];
comp2 = l[v[i].y];
if(comp1 != comp2){
ct++;
c+=v[i].c;
a[v[i].x][v[i].y] = v[i].c;
a[v[i].y][v[i].x] = v[i].c;
for(int j=0; j<n; j++){
if(l[j] == comp2){
l[j] = comp1;
}
}
}
}
}
void afisare(){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cout<<a[i][j]<<' ';
}
cout<<"\n";
}
}
int main(){
f>>n>>m;
citire();
ordonare_muchii();
kruskal();
afisare();
cout<<"Cost: "<<c;
f.close();
}