标签:
Description
Input
Output
Sample Input
Sample Output
1 #include<cstdio> 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 #define INF 999999999 6 int n; 7 int x[110],y[110],visit[110]; 8 double map[110][110],dis[110]; 9 double w(int i,int j) 10 { 11 return sqrt(1.0*((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]))); 12 } 13 void input() 14 { 15 int i,j; 16 double l; 17 scanf("%d",&n); 18 for(i=1;i<=n;i++) 19 scanf("%d%d",&x[i],&y[i]); 20 for(i=1;i<=n;i++) 21 for(j=1;j<=n;j++) 22 { 23 l=w(i,j); 24 if(l>=10&&l<=1000) 25 map[i][j]=map[j][i]=l; 26 else 27 map[i][j]=map[j][i]=INF; 28 } 29 return; 30 } 31 void prim() 32 { 33 int t=n,i,j,pos; 34 for(int i=2;i<=n;i++) 35 { 36 visit[i]=0; 37 dis[i]=map[i][1]; 38 } 39 visit[1] = 1; 40 double sum=0,temp; 41 while(t--) 42 { 43 temp=INF; 44 for(int j=1;j<=n;j++) 45 { 46 if(!visit[j]&&dis[j]<temp) 47 { 48 temp=dis[j]; 49 pos=j; 50 } 51 } 52 if(temp==INF)break; 53 visit[pos]=1; 54 sum+=dis[pos]; 55 for(int j=1;j<=n;j++) 56 { 57 if(!visit[j]&&dis[j]>map[pos][j]) 58 dis[j]=map[pos][j]; 59 } 60 } 61 if(t==0) 62 { 63 printf("%.1f\n",sum*100); 64 } 65 else printf("oh!\n"); 66 } 67 main() 68 { 69 int T; 70 scanf("%d",&T); 71 while(T--) 72 { 73 input(); 74 prim(); 75 } 76 }
标签:
原文地址:http://www.cnblogs.com/CrazyBaby/p/5440445.html