标签:
Time Limit: 3000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
Input
Output
Sample Input
4
0 0 0
0 1 1
1 1 2
1 0 3
0
Sample Output
1.000
Source
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const double eps=1e-4; 9 const int mxn=1005; 10 int n,m; 11 struct node{ 12 double x,y,z; 13 }p[mxn]; 14 double dist(node a,node b){ 15 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 16 } 17 struct edge{ 18 double dis; 19 double c; 20 }mp[mxn][mxn]; 21 double dis[mxn]; 22 bool vis[mxn]; 23 double prim(double r){ 24 int i,j; 25 memset(vis,0,sizeof vis); 26 for(i=1;i<=n;i++){dis[i]=1e9;} 27 dis[1]=0; 28 double res=0; 29 while(1){ 30 int pos=0;double mini=1e9; 31 for(i=1;i<=n;i++) 32 if(!vis[i] && dis[i]<mini){ 33 mini=dis[i]; 34 pos=i; 35 } 36 if(pos==0)break; 37 res+=dis[pos]; 38 dis[pos]=0;vis[pos]=1; 39 for(i=1;i<=n;i++){ 40 if(!vis[i]) 41 dis[i]=min(dis[i],mp[pos][i].c-r*mp[pos][i].dis); 42 } 43 } 44 return res; 45 } 46 int main(){ 47 while(scanf("%d",&n) && n){ 48 int i,j; 49 for(i=1;i<=n;i++) 50 scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z); 51 for(i=1;i<n;i++) 52 for(j=i+1;j<=n;j++){ 53 mp[i][j].dis=dist(p[i],p[j]); 54 mp[i][j].c=fabs(p[i].z-p[j].z); 55 mp[j][i]=mp[i][j]; 56 } 57 // double rd;//r=sum(c)/sum(dis) 58 double l=0;double r=200; 59 while(fabs(l-r)>eps){ 60 double mid=(l+r)/2; 61 // printf("testmessage: %.4f %.4f",l,r); 62 double tmp=prim(mid); 63 if(tmp>=0)l=mid; 64 else r=mid; 65 } 66 printf("%.3f\n",l); 67 } 68 return 0; 69 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5883428.html