第 34 题

克鲁斯卡尔求最小生成树思想:首先将 $ n $ 个点看做 $ n $ 个独立的集合,将所有边快排(从小到大)。然后,按排好的顺序枚举每一条边,判断这条边连接的两个点是否属于一个集合。若是,则将这条边加入最小生成树,并将两个点所在的集合合并为一个集合。若否,则跳过。直到找到 $
n−1 $ 条边为止。

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
#include <iostream>
#include <algorithm>
using namespace std;
struct point {
int x;
int y;
int v;
};
point a[10000];
​
int cmp(const point &a, const point &b) {
if ( ① ) return 1;
else return 0;
}
​
int fat[101];
int father(int x) {
if (fat[x] != x) return fat[x]= ② ;
else return fat[x];
}
​
void unionn(int x,int y) {
int fa = father(x);
int fb = father(y);
if (fa != fb) fat[fa] = fb;
}
​
int main() {
int i, j, n, m, k = 0, ans = 0, cnt = 0;
cin >> n;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
cin >> m;
if (m != 0) {
k++;
a[k].x = i;
a[k].y = j;
a[k].v = m;
}
}
sort(a + 1, a + 1 + k, ③ );
​
for (i = 1; i <= n; i++) {
fat[i] = i;
}
​
for (i = 1; i <= k; i++) {
if (father(a[i].x) != ④ ) {
ans += a[i].v;
unionn(a[i].x, a[i].y);
cnt++;
}
if ( ⑤ ) break;
}
​
cout << ans;
return 0;
​
}

请选择 ① 应该填写的代码

  • A. $ a.v > b.v $

  • B. $ a.v < b.v $

  • C. $ a.v <= b.v $

  • D. $ a.v >= b.v $