http://acm.hdu.edu.cn/showproblem.php?pid=1233 最小生成树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int G[105][105],dis[105],vis[105];
int dist=0;
int n;
void prim(){
vis[1]=1;
int m;
for(int i=1;i<=n;i++){
dis[i]=G[1][i];
}
while(true){
int minn=999999;
int m=0,flag=0;
for(int i=1;i<=n;i++){
if(minn>dis[i]&&vis[i]==0){
minn=dis[i];
m=i;
flag=1;
}
}
if(flag){
vis[m]=1;
dist+=minn;
}
else{
break;
}
for(int i=1;i<=n;i++){
if(dis[i]>G[m][i]&&(vis[i]==0)){
dis[i]=G[m][i];
}
}
}
}
int main(){
int a,b,c;
scanf("%d",&n);
while(n){
int t=(n*(n-1))/2;
dist=0;
memset(G,999999,sizeof(G));
for(int i=0;i<=n;i++){//!
dis[i]=999999;
vis[i]=0;
}
for(int i=0;i<t;i++){
scanf("%d%d%d",&a,&b,&c);
G[a][b]=c;
G[b][a]=c;
}
prim();
printf("%d\n",dist);
scanf("%d",&n);
}
return 0;
}