-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prim.cpp
76 lines (63 loc) · 1.44 KB
/
Prim.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
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define MAX 105
int n,m;
struct edge{
int u,v,cost;
edge(int a, int b, int c){
u=a;
v=b;
cost=c;
}
};
struct comp{
bool operator()(const edge &first, const edge &second){
return first.cost>second.cost;
}
};
vector< edge > graph[MAX];
int vis[MAX];
void prim(int s){
vector< int > vnew;
vector< edge > enew;
int u=s;
vnew.pb(u);
vis[u]=1;
priority_queue<edge, vector< edge >, comp> pq;
for(int i=0;i<graph[u].size();i++){
if(!vis[graph[u][i].v])
pq.push(graph[u][i]);
}
while(!pq.empty()){
if(vnew.size()==n) break;
edge e = pq.top();
pq.pop();
u = e.v;
if(!vis[u] && vis[e.u]){
vnew.pb(u);
enew.pb(e);
vis[u]=1;
for(int i=0;i<graph[u].size();i++){
if(!vis[graph[u][i].v])
pq.push(graph[u][i]);
}
}
}
for(int i=0;i<enew.size();i++){
printf("%d %d %d\n",enew[i].u, enew[i].v, enew[i].cost);
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v,cost;
cin>>u>>v>>cost;
graph[u].pb(edge(u,v,cost));
graph[v].pb(edge(v,u,cost));
}
memset(vis,0,sizeof vis);
prim(1);
cout<<endl;
return 0;
}