-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path79.cpp
More file actions
60 lines (47 loc) · 1.06 KB
/
79.cpp
File metadata and controls
60 lines (47 loc) · 1.06 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
// Prim MST : Priority_queue
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int ch[30];
struct edge{
int e;
int val;
edge(int a, int b){
e = a;
val = b;
}
bool operator<(const edge &b)const{
return val > b.val;
}
};
int main(){
freopen("input.txt", "rt", stdin);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int v, ed, a, b, c, ans = 0;
priority_queue<edge> q;
vector<pair<int, int> > map[30];
cin>>v>>ed;
for(int i = 0 ; i < ed; i++){
cin>>a>>b>>c;
map[a].push_back(make_pair(b, c));
map[b].push_back(make_pair(a, c));
}
q.push(edge(1, 0));
while(!q.empty()){
edge tmp = q.top();
q.pop();
int v = tmp.e;
int cost = tmp.val;
if(ch[v] == 0){
ans += cost;
ch[v] = 1;
for(int i = 0; i < map[v].size(); i++){
q.push(edge(map[v][i].first, map[v][i].second));
}
}
}
cout<<ans<<"\n";
return 0;
}