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.
经典的01分数规划:最优比率生成树问题。
设题目所求的比率为r=ΣC/ΣV,其中v表示价值,c表示费用。
Σc-Σv*r=0。
注意到这个式子可以写成Σ(c-v*r)=0。
于是可以二分答案r,对每个r求以c-v*r为边权的最小生成树,若答案大于0,说明这个r不可行,若答案小于0,说明答案可行,但还有更优解。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
const double eps=1e-4;
double val[1010][1010],cost[1010][1010],dis[1010];
int n,xx[1010],yy[1010],zz[1010];
bool in[1010];
void init()
{
int i,j,k;
for (i=1;i<=n;i++)
scanf("%d%d%d",&xx[i],&yy[i],&zz[i]);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
val[i][j]=sqrt((xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]));
cost[i][j]=fabs(zz[i]-zz[j]);
}
}
bool ok(double x)
{
int i,j,k,p,q;
double y,z,ans=0,minn;
dis[1]=0;
memset(in,0,sizeof(in));
in[1]=1;
for (i=2;i<=n;i++)
dis[i]=cost[1][i]-val[1][i]*x;
for (i=1;i<n;i++)
{
minn=0x3f3f3f3f;
for (j=1;j<=n;j++)
if (!in[j]&&dis[j]<minn)
{
minn=dis[j];
p=j;
}
in[p]=1;
ans+=dis[p];
for (j=1;j<=n;j++)
dis[j]=min(dis[j],cost[p][j]-val[p][j]*x);
}
return ans<=eps;
}
void solve()
{
int i,j,k,p,q;
double x,y,z,l,r,mid;
l=0;
r=1e7;
while (fabs(l-r)>=eps)
{
mid=(l+r)/2;
if (ok(mid)) r=mid;
else l=mid;
}
printf("%.3f\n",l);
}
int main()
{
while (scanf("%d",&n)&&n)
{
init();
solve();
}
}