Minimum spanning tree, simple topic. Set of prim templates, other topics are more meaningful.
Sample Input
3//Number of villages
1 2 1/connectivity status and weights
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0
Sample Output//The right to export the minimum spanning tree
3
5
1#include <stdio.h>2#include <string.h>3 #defineSize 1004 #defineINF 0x3f3f3f3f5 6 intVellage[size][size],flag[size];7 intN,T,A,B,C,I,J,K,ANS,MAXN;8 9 voidInit ()Ten { Onet=n* (n1) >>1; Amemset (Flag,0,sizeof(flag)); -memset (Vellage,0,sizeof(Vellage)); - } the - intPrim () - { -ans=0; + for(intH=1; h<=n;h++) - { +maxn=inf; A for(i=1; i<=n;i++) at { - if(flag[i]!=1&&vellage[1][i]<MAXN) - { -maxn=vellage[1][i]; -k=i; - } in } -ans+=MAXN; toflag[k]=1; + for(j=2; j<=n;j++) - { the if(flag[j]!=1&&vellage[k][j]<vellage[1][j]) *vellage[1][j]=Vellage[k][j]; $ }Panax Notoginseng } - returnans; the } + A intMain () the { + while(SCANF ("%d",&N), N) - { $ Init (); $ while(t--) - { -scanf" %d%d%d",&a,&b,&c); thevellage[a][b]=vellage[b][a]=C; - Wuyi } theprintf"%d\n", Prim ()); - } Wu return 0; -}
View Code
2016HUAS_ACM Summer Camp 3G-or smooth project