-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculatePrimsMST.cpp
41 lines (32 loc) · 1012 Bytes
/
calculatePrimsMST.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
#include <bits/stdc++.h>
vector<pair<pair<int, int>, int>> calculatePrimsMST(int n, int m, vector<pair<pair<int, int>, int>> &g)
{
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;
vector<vector<pair<int, int>>> adj(n+1);
for(auto pr:g) {
adj[pr.first.first].push_back({pr.first.second, pr.second});
adj[pr.first.second].push_back({pr.first.first, pr.second});
}
vector<int> vis(n+1, 0);
vis[1] = 1;
vector<pair<pair<int, int>, int>> ans;
for(auto pr:adj[1]) {
pq.push({pr.second, 1, pr.first});
}
while(ans.size() < n-1) {
int prev = pq.top()[1];
int cur = pq.top()[2];
int wt = pq.top()[0];
pq.pop();
if(vis[cur])
continue;
vis[cur] = 1;
ans.push_back({{prev, cur}, wt});
for(auto pr:adj[cur]) {
if(vis[pr.first])
continue;
pq.push({pr.second, cur, pr.first});
}
}
return ans;
}