-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy path11.4.cpp
87 lines (77 loc) · 2.13 KB
/
11.4.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
/*
* 题目名称:还是畅通工程
* 题目来源:浙江大学复试上机题
* 题目链接:http://t.cn/AiWud0C6
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100 + 10;
struct Edge {
int from; //边的起点
int to; //边的终点
int length; //边的长度
bool operator< (const Edge& e) const {
return length < e.length;
}
};
Edge edge[MAXN * MAXN]; //边集
int father[MAXN]; //父亲结点
int height[MAXN]; //结点高度
void Initial(int n) { //初始化
for (int i = 0; i <= n; i++) {
father[i] = i;
height[i] = 0;
}
}
int Find(int x) { //查找根结点
if (x != father[x]) {
father[x] = Find(father[x]);
}
return father[x];
}
void Union(int x, int y) { //合并集合
x = Find(x);
y = Find(y);
if (x != y) {
if (height[x] < height[y]) {
father[x] = y;
} else if (height[y] < height[x]) {
father[y] = x;
} else {
father[y] = x;
height[x]++;
}
}
return ;
}
int Kruskal(int n, int edgeNumber) {
Initial(n);
sort(edge, edge + edgeNumber); //按权值排序
int sum = 0;
for (int i = 0; i < edgeNumber; ++i) {
Edge current = edge[i];
if (Find(current.from) != Find(current.to)) {
Union(current.from, current.to);
sum += current.length;
}
}
return sum;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
if (n == 0) {
break;
}
int edgeNumber = n * (n - 1) / 2;
for (int i = 0; i < edgeNumber; ++i) {
scanf("%d%d%d", &edge[i].from, &edge[i].to, &edge[i].length);
}
int answer = Kruskal(n, edgeNumber);
printf("%d\n", answer);
}
return 0;
}