-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1197.cpp
51 lines (45 loc) · 812 Bytes
/
p1197.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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define PI pair<int, int>
struct cmp {
bool operator()(PI a, PI b) {
return a.second > b.second;
}
};
int v, en, a, b, c, val = 0;
vector<vector<PI>> edge;
priority_queue<PI, vector<PI>, cmp> e;
bool visit[10001];
void prim(int key) {
visit[key] = 1;
for (auto u : edge[key]) {
if (!visit[u.first])
e.push(u);
}
while (!e.empty()) {
PI f = e.top(); e.pop();
if(!visit[f.first]){
val += f.second;
prim(f.first);
return;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> v >> en;
edge.resize(v + 1);
for (int i = 0; i < en; i++) {
cin >> a >> b >> c;
edge[a].push_back({ b, c });
edge[b].push_back({ a,c });
}
prim(a);
cout << val << '\n';
return 0;
}