oh!
#include<stdio.h> #include<math.h> #include<string.h> #define N 1000000 double map[110][110]; struct qiao { int x,y; }qiao[110]; double between(int i,int j) { return sqrt((double)((qiao[i].y-qiao[j].y)*(qiao[i].y-qiao[j].y)+(qiao[i].x-qiao[j].x)*(qiao[i].x-qiao[j].x))); } double prim(int n) { int used[110]; int i,j,k; double min,sum=0; double dis[110]; memset(used,0,sizeof(used)); memset(dis,0,sizeof(dis)); for(i=1;i<=n;i++) { dis[i]=map[i][1]; } for(i=1;i<n;i++) { min=N; for(j=2;j<=n;j++) { if(min>dis[j] && used[j]==0) { min=dis[j]; k=j; } } if(min==N) return 0; sum+=min; used[k]=1; for(j=1;j<=n;j++) { if(used[j]==0 && dis[j]>map[k][j]) dis[j]=map[k][j]; } } return sum; } int main() { int n,m,i,j; double t,sum; scanf("%d",&m); while(m--) { scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d %d",&qiao[i].x,&qiao[i].y); for(j=1;j<=i;j++) { t=between(i,j); if(t>=10 && t<=1000) map[i][j]=map[j][i]=t; else map[i][j]=map[j][i]=N; } } sum=100*prim(n); if(sum==0) printf("oh!\n"); else printf("%.1lf\n",sum); } return 0; }
原文地址:http://blog.csdn.net/holyang_1013197377/article/details/40047549