-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmst_prims.cpp
More file actions
56 lines (45 loc) · 1.14 KB
/
mst_prims.cpp
File metadata and controls
56 lines (45 loc) · 1.14 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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dbl;
#define fr(x,a,b) for(ll x=a;x<b;x++)
#define pb push_back
#define mod 1000000007
#define gmax LLONG_MAX
#define gmin LLONG_MIN
ll mst_prims(ll source_node,bool marked[],vector< pair<ll,ll> > edges[]){
priority_queue< pair<ll,ll>, vector< pair<ll,ll> >, greater< pair<ll,ll> > > pq;
ll cost=0;
pq.push(make_pair(0,1));
pair<ll,ll> p;
ll x,y;
while(!pq.empty()){
p=pq.top();
pq.pop();
if(marked[p.second]==false){
cost+=p.first;
marked[p.second]=true;
fr(i,0,edges[p.second].size()){
if(marked[edges[p.second][i].second]==false) pq.push(edges[p.second][i]);
}
}
}
return cost;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
ll n,m;
cin>>n>>m;
vector< pair<ll,ll> > edges[n+1];
ll weight,node1,node2;
fr(i,0,m){
cin>>node1>>node2>>weight;
edges[node1].pb(make_pair(weight,node2));
edges[node2].pb(make_pair(weight,node1));
}
bool marked[n+1]={false};
// We are calculating mst from node1 but any node can be used
cout<<mst_prims(1,marked,edges);
return 0;
}