-
Notifications
You must be signed in to change notification settings - Fork 0
/
MST_Prims.cpp
85 lines (70 loc) · 1.59 KB
/
MST_Prims.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
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
class Graph
{
private:
// Adjacency list
vector<int, int> *l;
int V;
public:
Graph()
{
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 prim_mst()
{
// most important stuff
// init a min heap
priority_queue<pair<int, int>>, vector<pair<int, int>, greater<pair<int, int>>> Q;
// another array
bool *visited = new bool[V]{0};
int ans = 0;
// begin
Q.push({0,0}); // weight , node
while (!Q.empty())
{
// pick out the edge with minimum weight
auto best = Q.top();
Q.pop();
int to = best.second;
int weight = best.first;
if (visited[to])
{
// discard the edge and continue;
continue;
}
// otherwise take the current edge
ans+= weight;
visited[to] = 1;
// add the new edges in the queue
for (auto x:l[to])
{
if (visited[x.first]==0)
{
Q.push(x.second,x.first);
}
}
}
return ans;
}
};
int main()
{
int n,m;
cin>>n>>m;
Graph g(n);
for (int i = 0; i < m; i++)
{
int x,y,w;
cin>>x>>y>>w;
g.addEdge(x-1,y-1,w);
}
cout<<g.prim_mst()<<endl;
return 0;
}