标签:
1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 #include<iomanip> 5 #define MAXN 250 6 #define inf 1000000000 7 typedef double elem_t; 8 9 using namespace std; 10 11 double _m[MAXN][MAXN]; 12 struct point 13 { 14 double a; 15 double b; 16 }; 17 elem_t prim(int n,elem_t mat[][MAXN],int* pre); 18 point p[250]; 19 int pre[250]; 20 int main() 21 { 22 //freopen("acm.acm","r",stdin); 23 int num; 24 int max = 0; 25 int i; 26 int time = 0; 27 int j; 28 int value; 29 while(cin>>num) 30 { 31 if(!num) 32 break; 33 cin>>p[0].a; 34 cin>>p[0].b; 35 cin>>p[1].a; 36 cin>>p[1].b; 37 memset(_m,0,sizeof(_m)); 38 for(i = 2; i < num; ++ i) 39 { 40 cin>>p[i].a; 41 cin>>p[i].b; 42 } 43 for(i = 0; i < num; ++ i) 44 { 45 for(j = i; j < num; ++ j) 46 { 47 _m[i][j] = (p[i].a - p[j].a)*(p[i].a - p[j].a) + (p[i].b - p[j].b)*(p[i].b - p[j].b); 48 _m[j][i] = _m[i][j]; 49 } 50 } 51 prim(num,_m,pre); 52 i = 1; 53 while(1) 54 { 55 if(_m[i][pre[i]] > max) 56 { 57 max = _m[i][pre[i]]; 58 } 59 if(pre[i] == 0) 60 break; 61 i = pre[i]; 62 } 63 cout<<"Scenario #"<<++ time<<endl 64 <<"Frog Distance = " 65 <<setiosflags(ios::fixed)<<setprecision(3)<<sqrt(long double (max))<<endl 66 <<endl; 67 max = 0; 68 } 69 70 } 71 72 73 74 elem_t prim(int n,elem_t mat[][MAXN],int* pre){ 75 elem_t min[MAXN],ret=0; 76 int v[MAXN],i,j,k; 77 for (i=0;i<n;i++) 78 min[i]=inf,v[i]=0,pre[i]=-1; 79 for (min[j=0]=0;j<n;j++){ 80 for (k=-1,i=0;i<n;i++) 81 if (!v[i]&&(k==-1||min[i]<min[k])) 82 k=i; 83 for (v[k]=1,ret+=min[k],i=0;i<n;i++) 84 if (!v[i]&&mat[k][i]<min[i]) 85 min[i]=mat[pre[i]=k][i]; 86 } 87 return ret; 88 }
标签:
原文地址:http://www.cnblogs.com/gavinsp/p/4566744.html