-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim_mst.cpp
More file actions
47 lines (41 loc) · 1.13 KB
/
prim_mst.cpp
File metadata and controls
47 lines (41 loc) · 1.13 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
ll n , m , tot_wt , e;
vector<vector<pll>>graph , tree;
vll parent , dist;
void prim_mst(ll source ){
set<pll>st;
st.insert({0,source});
dist[source] = 0;
vector<bool>vis(n+1 , false);
while(!st.empty()){
auto x = *st.begin();
st.erase(x);
ll u = x.second;
if(vis[u]){
continue;
}
ll v = parent[u];
ll w = x.first;
tree[v].pb({w , u});
tree[u].pb({w , v});
e++;
vis[u] = true;
tot_wt += x.first;
for(auto edge: graph[u]){
if(!vis[edge.second] && edge.first < dist[edge.second]){
st.erase({dist[edge.second] , edge.second});
dist[edge.second] = edge.first;
parent[edge.second] = u;
st.insert({edge.first , edge.second});
}
}
}
}
// if e != n , then it is impossible to form the mst
void init( ){
graph.resize(n+1);
parent.assign(n+1 ,0);
dist.assign(n+1 , INF);
tree.resize(n+1);
}
//good q: https://codeforces.com/contest/609/problem/E
//https://codeforces.com/contest/609/submission/186681679