-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathMst Prims Algorithm.cpp
More file actions
66 lines (57 loc) · 1.13 KB
/
Copy pathMst Prims Algorithm.cpp
File metadata and controls
66 lines (57 loc) · 1.13 KB
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
#include <bits//stdc++.h>
using namespace std;
int visited[1000];
vector<int>edge[1000];
vector<int>cost[1000];
struct data
{
int v, w;
bool operator < (data p) const {
return w > p.w;
}
};
int mstPrims(int src, int n)
{
memset(visited, 0, sizeof(visited));
priority_queue<data>pq;
int u, v, p, sum = 0;
for(int i = 0; i < n-1; i++)
{
u = src;
visited[src] = 1;
for(int j = 0; j < edge[u].size(); j++)
{
v = edge[u][j];
if(visited[v] == 0)
{
data D;
D.v = v;
D.w = cost[u][j];
pq.push(D);
}
}
while(visited[src])
{
data T = pq.top(); pq.pop();
src = T.v;
p = T.w;
}
sum += p;
}
return sum;
}
int main()
{
int n, e, u, v, w;
scanf("%d %d", &n, &e);
for(int i = 0; i < e; i++)
{
cin>>u>>v>>w;
edge[u].push_back(v);
edge[v].push_back(u);
cost[u].push_back(w);
cost[v].push_back(w);
}
cout<<mstPrims(1, n);
return 0;
}