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.
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
【题意】最优比例树,给了n个3维空间的坐标,求一颗树,使得高度之和与水平距离之和的比最大
【AC代码】
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
double x,y,z;
}a[1010];
double get_dis(double x1,double y1,double x2,double y2)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
const int maxn = 1001;
int n;
bool done[maxn];
double g1[maxn][maxn],g2[maxn][maxn],d[maxn];
double check(double data)
{
int i,j,tj;
double temp,ans;
for(int i=2; i<=n; i++){
done[i]=false;
d[i]=g2[1][i]-g1[1][i]*data;
}
ans=0,done[1]=true;
d[1]=0;
for(i=1; i<n; i++){
temp=1e30,tj=0;
for(j=2; j<=n; j++){
if(!done[j]&&d[j]<temp){
tj=j;
temp=d[j];
}
}
done[tj]=true;
ans += d[tj];
for(int j=2; j<=n; j++)if(!done[j])d[j]=min(d[j],g2[tj][j]-g1[tj][j]*data);
}
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
for(int i=1; i<=n; i++)
{
scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z);
}
memset(g1,0,sizeof(g1));
memset(g2,0,sizeof(g2));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n;j++)
{
if(i!=j)
{
g1[i][j] = g1[j][i] = get_dis(a[i].x,a[i].y,a[j].x,a[j].y);
g2[i][j] = g2[j][i] = fabs(a[i].z-a[j].z);
}
}
}
double small,mid,big;
small = 0,big = 1e6;
for(int i=1; i<=50 ;i++)
{
mid = (big+small)/2;
if(check(mid)<0)big=mid;
else small=mid;
}
printf("%.3f\n",(big+small)/2);
}
return 0;
}