-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP3366_PrimMST.cpp
73 lines (59 loc) · 1.12 KB
/
P3366_PrimMST.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
#include<iostream>
#include<cmath>
#define MAXN 5005
#define MAXM 200005
#define INF 233333
//Prim Minimum Spanning Tree
using namespace std;
//non direction, double size
struct edge{
int v,w,next;
}e[MAXN<<1];
int head[MAXM],dis[MAXN],cnt,n,m,tot=1,now=1,ans;
bool vis[MAXM];
void add(int u,int v,int w){
e[++cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
int prim(){
//give dis max value
for(int i=2;i<=n;i++)
dis[i]=INF;
//double path so min
for(int i=head[1];i;i=e[i].next)
dis[e[i].v]=min(dis[e[i].v],e[i].w);
while(tot++<n){
//MST path # == edge-1
int minn=INF;
vis[now]=1;//visited
for(int i=1;i<=n;++i){
if(!vis[i]&&minn>dis[i]){
minn=dis[i];
now=i;
}
}
ans+=minn;
//all edge that link to now, refresh dis
for(int i=head[now];i;i=e[i].next){
int v=e[i].v;
if(dis[v]>e[i].w&&!vis[v])
dis[v]=e[i].w;
}
}
return ans;
}
int main(){
cin>>n>>m;
int u,v,w;
for(int i=1;i<=m;i++){
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
prim();
cout<<cnt<<endl;
cout<<ans;
return 0;
}