-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprim_heap.cpp
53 lines (47 loc) · 1.17 KB
/
prim_heap.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
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
//考虑无向图M = 4 * N
const int N = 1e5 + 10, M = 4 * N, INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
int n, m, h[N], e[M], w[M], ne[M], idx;
bool st[N];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int prim() {
int res = 0, cnt = 0;
priority_queue<PII, vector<PII>, greater<> > heap;
heap.push({0, 1});
while (heap.size()) {
auto t = heap.top();
heap.pop();
int dis = t.first, ver = t.second;
if (st[ver]) continue;
st[ver] = true, res += dis, cnt++;
for (int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
if (st[j]) continue;
heap.push({w[i], j});
}
}
if (cnt != n) return INF;
return res;
}
int main() {
memset(h, -1, sizeof h);
cin >> n >> m;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
// 最小生成树不考虑自环
if (a == b) continue;
add(a, b, c);
add(b, a, c);
}
int t = prim();
if (t == INF) cout << "impossible";
else cout << t;
return 0;
}