-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMinimum Spanning Tree ( prim's).cpp
52 lines (49 loc) · 1.18 KB
/
Minimum Spanning Tree ( prim's).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
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef pair<long long , int> pii;
bool marked[1000];
vector< pii > adj[1000];
long long prim(int x)
{
priority_queue<pii, vector<pii> , greater<pii> > pq;
int y;
long long minimumCost=0;
pii p;
pq.push(make_pair(0,x));
while(!pq.empty())
{
p = pq.top();
pq.pop();
x = p.second;
if(marked[x]==true)
continue;
minimumCost += p.first;
marked[x] = true;
for(int i=0; i< adj[x].size();i++)
{
y = adj[x][i].second;
if(marked[y]==false)
pq.push(adj[x][i]);
}
}
return minimumCost;
}
int main()
{
int nodes, edges,x,y;
long long weight,minimumCost;
cout<<"Enter number of nodes and edges\n";
cin>>nodes>>edges;
cout<<"Vertex to vertex and its weight\n";
for(int i=0;i<edges;i++)
{
cin>>x>>y>>weight;
adj[x].push_back(make_pair(weight,y));
adj[y].push_back(make_pair(weight,x));
}
minimumCost = prim(1);
cout<<"Minimum cost is "<<minimumCost<<endl;
return 0;
}