-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXay dung cay bao trum be nhat theo Kruskal.cpp
110 lines (79 loc) · 2.53 KB
/
Xay dung cay bao trum be nhat theo 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#include <set>
typedef long long ll;
typedef long double ld;
#define FOR(i, a) for (int i = 0; i < (a); i++)
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
const int MOD = 1000000007;
using namespace std;
int kq = 0;
int a[1005][1005];
int check[1005];
int n;
struct canh {
int u, v, w;
bool operator < (const canh &a) const {
if (w == a.w) {
if (u == a.u) {
return v < a.v;
}
return u < a.u;
}
return w < a.w;
}
};
vector<canh> edges;
//dsu
int par[1005];
int find(int u) {
if (par[u] == u) return u;
return par[u] = find(par[u]);
}
void join(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return;
par[u] = v;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
par[i] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] && i < j) {
edges.push_back({i, j, a[i][j]});
}
}
}
sort(all(edges));
vector<pair<int, int>> s;
for (auto i : edges) {
if (find(i.u) != find(i.v)) {
join(i.u, i.v);
kq += i.w;
s.push_back({i.u, i.v});
}
}
cout << "dh = " << kq << "\n";
for (auto i : s) {
cout << i.f << " " << i.s << endl;
}
}