首页 > 代码库 > POJ2728 Desert King

POJ2728 Desert King

 
Time Limit: 3000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

Description

David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way. 

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

There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.

Sample Input

40 0 00 1 11 1 21 0 30

Sample Output

1.000

Source

Beijing 2005
 
最优比率生成树。
设花费(海拔差)和为c,距离和为dis,则有比率r=dis/c
设最优比率R=c/dis,则有R*dis=c
我们可以二分尝试r,算最小生成树。若c-r*dis=0,则正好r=R,若大于0,则r过小,若小于0,则r还可以更小。
 
因为随着r不同,生成树的“边权”也不同,所以适合用prim做。用kruskal的话不方便对边排序。
 
 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 }

 

POJ2728 Desert King