-
Notifications
You must be signed in to change notification settings - Fork 492
/
primsPriorityQueue.cpp
80 lines (62 loc) Β· 1.72 KB
/
primsPriorityQueue.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Graph{
//First node second weight
vector<pair<int,int>> *l;
int V;
public:
Graph(int n){
V=n;
l=new vector<pair<int,int>>[n];
}
void addEdge(int x,int y,int w){
l[x].push_back({y,w});
l[y].push_back({x,w});
}
int prims(){
//Most most important stuff
//See priority queue implementation
//First element weight second node , queue sorts on basis of first ele
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> Q;
//visited array that denotes whether a node is included of not in mst
//V vertices bool array all false i.e 0
bool * visited = new bool[V]{0};
int ans =0;
//0 based indexing
Q.push({0,0});
while(!Q.empty()){
//Pick out the edge with minimum weight
auto best=Q.top();
Q.pop();
int dest=best.second;
int weight=best.first;
if(visited[dest]){
//Discarding the edge
continue;
}
ans+=weight;
visited[dest]=1;
//adding neighbours in the queue
for(auto x:l[dest]){
if(visited[x.first]==0){
Q.push({x.second,x.first});
}
}
}
return ans;
}
};
int main(){
int n,m,x,y,w;
cin>>n>>m;
Graph g(n);
for (int i = 0; i <m; i++)
{
cin>>x>>y>>w;
g.addEdge(x-1,y-1,w);
}
cout<<g.prims()<<endl;
return 0;
}