-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1675-RoadConstruction.cpp
74 lines (53 loc) · 1.22 KB
/
1675-RoadConstruction.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// JAI BAJARANG BALI
// manitianajay45
// give me some sunshine, give me some rain , give me another chance to grow up once again....
// sab moh maya hai....
/*
dijkastra.
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
ll n,m;
vector<pair<ll,ll>> graph[200005];
vector<bool> visited(200005, false);
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// ll n;
cin >> n>>m;
for (ll i = 0; i <m; i++)
{
ll u, v,c;
cin >> u >> v>>c;
graph[u].push_back({v,c});
graph[v].push_back({u,c});
}
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
pq.push({0,1});
ll ans=0;
while(!pq.empty()){
pair<ll,ll> pr=pq.top();
pq.pop();
if(visited[pr.second]){
continue;
}
visited[pr.second]=true;
ans+=pr.first;
for(auto i:graph[pr.second]){
if(!visited[i.first]){
pq.push({i.second,i.first});
}
}
}
for(ll i=1;i<=n;i++){
if(!visited[i]){
cout<<"IMPOSSIBLE"<<endl;
return 0;
}
}
cout<<ans<<endl;
return 0;
}