Desert King
Time Limit: 3000MS Memory Limit: 65536K
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
4
0 0 0
0 1 1
1 1 2
1 0 3
0
Sample Output
1.000
Source
Beijing 2005
/*
假设sigma(h)/sigma(l)==K 均值K可取,即: sigma(h)==K*sigma(l)
sigma(h)==K*(l1+l2+l3+...lm)
sigma(h)==K*l1+K*l2+K*l3+...K*lm
把原来的每个边的h都减去K*l
即hi'=hi-li'==hi-li*K
二分 K
这东西叫最优比率生成树
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
#define MAXN 1010
#define INF 9999999999
//const double INF = 99999999.0;
struct Node{ double x,y,h; }node[MAXN];
double map[MAXN][MAXN],h[MAXN][MAXN],l[MAXN][MAXN];
int n;
double head,tail;
double DIS_EUCLID(Node &a,Node &b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double PRIM(){
double ans=0.0,min;
double dis[MAXN];
int vis[MAXN];
for(int i=1;i<=n;i++){
dis[i]=map[1][i];
vis[i]=0;
}
dis[1]=1;vis[1]=1;
for(int i=2;i<=n;i++){
min=INF; int k;
for(int j=1;j<=n;j++)
if(!vis[j]&&dis[j]<min) min=dis[j],k=j;
vis[k]=1;
ans+=min;
for(int j=1;j<=n;j++)
if(!vis[j]&&dis[j]>map[k][j]) dis[j]=map[k][j];
}
return ans;
}
int main(){
double maxh,minh,maxl,minl,mid;
while(scanf("%d",&n),n){
maxh=maxl=-INF;
minh=minl=INF;
for(int i=1;i<=n;i++)
scanf("%lf%lf%lf",&node[i].x,&node[i].y,&node[i].h);
for(int i=1;i<=n-1;i++)
for(int j=i+1;j<=n;j++){
h[i][j]=h[j][i]=fabs(node[i].h-node[j].h);
double ans=DIS_EUCLID(node[i],node[j]);
l[i][j]=l[j][i]=ans;
if(maxh<h[i][j]) maxh=h[i][j];
if(maxl<l[i][j]) maxl=l[i][j];
if(minh>h[i][j]) minh=h[i][j];
if(minl>l[i][j]) minl=l[i][j];
}
head=minh/maxl;
tail=maxh/minl;
while(tail-head>1e-4){
mid=(tail+head)/2;
for(int i=1;i<=n-1;i++)
for(int j=i+1;j<=n;j++)
map[i][j]=map[j][i]=(h[i][j]-mid*l[i][j]);
if(PRIM()<=0.0) tail=mid;
else head=mid;
}
printf("%.3f\n",head);
}
return 0;
}
/*
4
0 0 0
0 1 1
1 1 2
1 0 3
0
*/