forked from joy-mollick/Problem-Solving-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HackerEarth-Minimum Spanning Tree.cpp
59 lines (48 loc) · 1.06 KB
/
HackerEarth-Minimum Spanning Tree.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e5+5;
typedef pair<int,int>P;
priority_queue<P,vector<P>,greater<P> >pq;
bool visited[MAX]={false};
vector<P>adj[MAX];
long minimum_cost=0;
void prim(int src)
{
pq.push(make_pair(0,src));
minimum_cost=0;
P pr;
while(!pq.empty())
{
int u=pq.top().second;
pr=pq.top();
pq.pop();
if(visited[u]==false) visited[u]=true;
else
continue;
minimum_cost+=pr.first;
vector<P>::iterator it;
for(it=adj[u].begin();it!=adj[u].end();it++)
{
int v= (*it).first;
int weight=(*it).second;
if(visited[v]==false)
{
pq.push(make_pair(weight,v));
}
}
}
}
int main()
{
int N,M,u,v,w;
cin>>N>>M;
while(M--)
{
cin>>u>>v>>w;
adj[u].push_back(make_pair(v,w));
adj[v].push_back(make_pair(u,w));
}
prim(1);
cout<<minimum_cost<<endl;
return 0;
}