-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrim.cpp
More file actions
66 lines (58 loc) · 1.14 KB
/
Prim.cpp
File metadata and controls
66 lines (58 loc) · 1.14 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
#include <iostream>
#include <vector>
using namespace std;
int n, s;
int a[100][100], d[100], e[100], vs[100];
const int inf = 1000;
void Prim(int s) {
for (int v = 1; v <= n; v++) {
d[v] = a[s][v];
e[v] = s;
vs[v] = 0;
}
d[s] = 0, e[s] = 0, vs[s] = 1;
int dem = 1, Wt = 0;
vector<pair<int, int>> v;
while (dem < n) {
int u = 0, Min = 100000;
for (int i = 1; i <= n; i++) {
if (Min > d[i] && vs[i] == 0) {
Min = d[i];
u = i;
}
}
if (u == 0) break;
Wt += a[u][e[u]];
v.push_back({ e[u], u });
vs[u] = 1;
for (int v = 1; v <= n; v++) {
if (vs[v] == 0 && d[v] > a[u][v]) {
d[v] = a[u][v];
e[v] = u;
}
}
dem++;
}
if (dem < n) cout << "Khong co cay khung\n";
else {
cout << "Wt = " << Wt << endl;
cout << "T = {";
for (int i = 0; i < v.size(); i++) {
auto x = v[i];
cout << "(" << x.first << "," << x.second << ")";
if (i != v.size() - 1) cout << ";";
}
cout << "}\n";
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
cin >> s;
Prim(s);
return 0;
}