-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1922.cpp
More file actions
41 lines (32 loc) · 917 Bytes
/
Copy path1922.cpp
File metadata and controls
41 lines (32 loc) · 917 Bytes
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
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
vector<pair<int, int>> v[1010];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> pq;
int visited[1010] = {0};
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio();
int N, M, a, b, c;
cin >> N >> M;
for(int i=0; i<M; i++){
cin >> a >> b >> c;
v[a].push_back({c, b});
v[b].push_back({c, a});
}
int sumWeights = 0;
int numVisited = 1;
int lastVisited = 1;
visited[1] = true;
while(numVisited < N){
for(auto node : v[lastVisited]) if(!visited[node.second]) pq.push(node);
while(visited[pq.top().second]) pq.pop();
sumWeights += pq.top().first;
visited[pq.top().second] = true;
lastVisited = pq.top().second;
pq.pop();
numVisited++;
}
cout << sumWeights;
}