-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkruskal.cpp
More file actions
56 lines (51 loc) · 1.29 KB
/
Copy pathkruskal.cpp
File metadata and controls
56 lines (51 loc) · 1.29 KB
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
#include <bits/stdc++.h>
#define N 10
#define M 20
#define INF 0x3f3f3f3f
using namespace std;
int n, m; // n是点数,m是边数
int p[N]; // 并查集的父节点数组
struct Edge{
int a, b, w;
bool operator < (const Edge &W) const{
return w < W.w;
}
}edges[M];
// 并查集核心操作
int find(int x){
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
//复杂度O(mlogm)
// 在图里面贪心找最小边,最终找到最小生成树,判断是否联通用并查集来解决
int kruskal(){
sort(edges, edges + m);
for(int i = 1; i <= n; i++){
p[i] = i; // 初始化并查集
}
int res = 0, cnt = 0;
for(int i = 0; i < m; i++){
int a = edges[i].a, b = edges[i].b, w = edges[i].w;
a = find(a), b = find(b);
// 如果两个连通块不连通,则将这两个连通块合并
if(a != b){
p[a] = b;
res += w;
cnt++;
}
}
if(cnt < n - 1) return INF;
return res;
}
int main(){
cin >> n >> m;
int a, b, w; // a到b,权重为w
for (int i = 0; i < m; i++) {
cin >> a >> b >> w;
edges[i].a = a;
edges[i].b = b;
edges[i].w = w;
}
cout << kruskal() << endl;
return 0;
}