Time Limit: 3000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital.
His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can't share a lifter. Channels can intersect safely and no three villages are on the same line.
As King David's prime scientist and programmer, you are asked to find out the best solution to build the channels.
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 }