-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPrim's mst.cpp
71 lines (71 loc) · 1.34 KB
/
Prim's mst.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
#include<vector>
#include<algorithm>
using namespace std;
struct edge
{
int u, v;
long long w;
edge(int p=0,int q=0,long long r=0):u(p),v(q),w(r){}
bool operator<( const edge &v2)const
{
return w > v2.w;
}
};
class PRIM_s
{
/*
In Prim's algorithm the safe edge is added one by one while its running time is O(ElgE)
*/
vector<bool> inQ;
vector<edge> heap;
vector<vector<int> > adj, adjW;
public:
PRIM_s(int n = 0):inQ(n),adj(n),adjW(n){}
long long Prims()
{
edge tmp;
int u;
long long A = 0;
for (int i = 0; i < adj[0].size(); i++)
{
tmp.u = 0, tmp.v = adj[0][i], tmp.w = adjW[0][i];
heap.push_back(tmp);
push_heap(heap.begin(), heap.end());
}
inQ[0] = true;
for(int z=0;z<adj.size()-1;z++)
{
tmp = heap.front();
while (inQ[tmp.v])
{
pop_heap(heap.begin(), heap.end());
heap.pop_back();
tmp = heap.front();
}
inQ[tmp.v] = true;
pop_heap(heap.begin(), heap.end());
heap.pop_back();
A += tmp.w;
u = tmp.v;
tmp.u = u;
for (int i = 0; i < adj[u].size(); i++)
{
if (!inQ[adj[u][i]])
{
tmp.v = adj[u][i], tmp.w = adjW[u][i];
heap.push_back(tmp);
push_heap(heap.begin(), heap.end());
}
}
}
return A;
}
bool add(int u, int v, long long w)
{
adj[u].push_back(v);
adjW[u].push_back(w);
adj[v].push_back(u);
adjW[v].push_back(w);
return true;
}
};