-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoad Reparation.cpp
More file actions
65 lines (52 loc) · 1.31 KB
/
Road Reparation.cpp
File metadata and controls
65 lines (52 loc) · 1.31 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
60
61
62
63
64
65
// Alhamdulillah
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define eb emplace_back
#define forr(i, a, b) for (ll i = a; i < b; i++)
const ll mod = 1e9 + 7;
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
int main() {
fast;
ll n, m, x, y, i, c;
cin >> n >> m;
vector<vector<pair<ll, ll>>> adj_list(n + 1);
vector<ll> vis(n + 1);
vector<ll> value(n + 1, LONG_LONG_MAX);
for (i = 1; i <= m; i++) {
cin >> x >> y >> c;
adj_list[x].push_back({y, c});
adj_list[y].push_back({x, c});
}
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
value[1] = 0;
pq.push({value[1], 1});
ll ans = 0;
while (!pq.empty()) {
auto [c, curr] = pq.top();
pq.pop();
if (vis[curr])
continue;
vis[curr] = 1;
ans += c;
for (auto [neigh, w] : adj_list[curr]) {
if (vis[neigh])
continue;
if (w < value[neigh]) {
value[neigh] = w;
pq.push({w, neigh});
}
}
}
vis[0] = 1;
forr(i, 1, n + 1) {
if (!vis[i]) {
cout << "IMPOSSIBLE";
return 0;
}
}
cout << ans;
}