- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims.cpp
More file actions
Latest commit
50 lines (49 loc) · 942 Bytes
/
Copy pathprims.cpp
File metadata and controls
50 lines (49 loc) · 942 Bytes
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
#include<iostream>
#include<vector>
#include<queue>
#include<functional>
#include<utility>
usingnamespacestd;
constintMAX=1e4 + 5;
typedef pair< longlong ,int> PII;
bool marked[MAX];
vector <PII> adj[MAX];
longlongintprim(int x)
{
priority_queue<PII, vector<PII>, greater<PII> >Q;
int y;
longlong minimumCost = 0;
PII p;
Q.push(make_pair(0, x));
while(!Q.empty())
{
p = Q.top();
Q.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)
Q.push(adj[x][i]);
}
}
return minimumCost;
}
intmain(void)
{
int nodes, edges, x, y;
longlong weight, minimumCost;
cin>>nodes>>edges;
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<<minimumCost<<endl;
}