-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal.cpp
More file actions
68 lines (54 loc) · 1.67 KB
/
Kruskal.cpp
File metadata and controls
68 lines (54 loc) · 1.67 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
57
58
59
60
61
62
63
64
65
66
67
68
//Cre:VNOI
#include <bits/stdc++.h>
using namespace std;
// Cấu trúc để lưu các cạnh đồ thị
// u, v là 2 đỉnh, c là trọng số cạnh
struct Edge {
int u, v, c;
Edge(int _u, int _v, int _c): u(_u), v(_v), c(_c) {};
};
struct Dsu {
vector<int> par;
void init(int n) {
par.resize(n + 5, 0);
for (int i = 1; i <= n; i++) par[i] = i;
}
int find(int u) {
if (par[u] == u) return u;
return par[u] = find(par[u]);
}
bool join(int u, int v) {
u = find(u); v = find(v);
if (u == v) return false;
par[v] = u;
return true;
}
} dsu;
// n và m là số đỉnh và số cạnh
// totalWeight là tổng trọng số các cạnh trong cây khung nhỏ nhất
int n, m, totalWeight = 0;
vector < Edge > edges;
int main() {
// Fast IO
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, c;
cin >> u >> v >> c;
edges.push_back({u, v, c});
}
dsu.init(n);
// Sắp xếp lại các cạnh theo trọng số tăng dần
sort(edges.begin(), edges.end(), [](Edge & x, Edge & y) {
return x.c < y.c;
});
// Duyệt qua các cạnh theo thứ tự đã sắp xếp
for (auto e : edges) {
// Nếu không hợp nhất được 2 đỉnh u và v thì bỏ qua
if (!dsu.join(e.u, e.v)) continue;
// Nếu hợp nhất được u, v ta thêm trọng số cạnh vào kết quả
totalWeight += e.c;
}
// Xuất ra kết quả
cout << totalWeight << '\n';
}