-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims.cpp
More file actions
45 lines (34 loc) · 918 Bytes
/
prims.cpp
File metadata and controls
45 lines (34 loc) · 918 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
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
#define pi pair<int, int>
int main()
{
int n, e;
cin >> n >> e;
vector<pi> v[n+5]; // {weight, node}
while (e--) {
int x, y, wt;
cin >> x >> y >> wt;
v[x].push_back({wt, y});
v[y].push_back({wt, x}); // Undirected
}
int src = 1;
vector<bool> vis(n+5, false);
priority_queue<pi, vector<pi>, greater<pi>> pq;
pq.push({0, src}); // {cost, node}
int ttl_cost = 0;
// Prim's MST
while (!pq.empty()) {
auto [cur_wt, cur_node] = pq.top(); // unpack directly
pq.pop();
if (vis[cur_node]) continue;
vis[cur_node] = true;
ttl_cost += cur_wt;
for (auto [ch_wt, ch_node] : v[cur_node]) { // unpack directly
if (!vis[ch_node]) {
pq.push({ch_wt, ch_node});
}
}
}
cout << ttl_cost << endl;
}