-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1922(2).cpp
45 lines (39 loc) · 1.03 KB
/
1922(2).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
//prim,pq+vec (advanced)
#include <bits/stdc++.h>
using namespace std;
#define INF 987654321
int n, m, u, v, w, d[100010], visited[100010], ans, max_node = 0, max_node_val = 0;
vector<pair<int, int>> vec[100010];
priority_queue<pair<int, int>> pq;
int main (){
scanf("%d %d", &n, &m);
while(m--){
scanf("%d %d %d", &u, &v, &w);
vec[u].push_back(make_pair(v,w));
vec[v].push_back(make_pair(u,w));
}
for(int i = 0; i <= n; i++){
// d[i] = INF;
visited[i] = 0;
}
pq.push({0,1});
while(!pq.empty()){
int x = pq.top().second;
// cout << x << "\n";
int y = -pq.top().first;
pq.pop();
if(visited[x] == 1) continue;
visited[x] = 1;
ans += y;
for(int i = 0; i < vec[x].size(); i++){
int newNode = vec[x][i].first;
int cost = vec[x][i].second;
if(visited[newNode]==0)
{
pq.push({-cost,newNode});
}
}
}
printf("%d", ans);
return 0;
}