POJ 2728 Desert King

来源:互联网 发布:推荐淘宝上燕窝 编辑:程序博客网 时间:2024/05/28 19:23

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


经典的01分数规划:最优比率生成树问题。
设题目所求的比率为r=ΣC/ΣV,其中v表示价值,c表示费用。
Σc-Σv*r=0。
注意到这个式子可以写成Σ(c-v*r)=0。
于是可以二分答案r,对每个r求以c-v*r为边权的最小生成树,若答案大于0,说明这个r不可行,若答案小于0,说明答案可行,但还有更优解。


#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;const int maxn=1005;int n,m;double xx[maxn],yy[maxn],zz[maxn],dis[maxn][maxn],cst[maxn][maxn],d[maxn];bool b[maxn];double prim(double lim){    memset(b,0,sizeof(b));    double sum=0;    for(int i=2;i<=n;i++)        d[i]=cst[1][i]-lim*dis[1][i];    b[1]=1;    int k;    for(int i=1;i<=n-1;++i)    {        double mn=1e9+7;        for(int j=1;j<=n;j++)            if(!b[j]&&d[j]<mn)            {                mn=d[j];                k=j;            }        b[k]=1;        sum+=mn;        for(int j=1;j<=n;j++)            if(!b[j]&&cst[k][j]-lim*dis[k][j]<d[j])                d[j]=cst[k][j]-lim*dis[k][j];    }    return sum;}int main(){    while(scanf("%d",&n)&&n)    {        for(int i=1;i<=n;i++)            scanf("%lf%lf%lf",xx+i,yy+i,zz+i);        for(int i=1;i<=n;i++)            for(int j=1;j<=n;++j)            {                dis[i][j]=sqrt((xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]));                cst[i][j]=fabs(zz[i]-zz[j]);            }        double l=0,r=100000,mid,tmp;        while(r-l>1e-4)        {            mid=(l+r)/2;            tmp=prim(mid);            if(tmp>=0)                l=mid;            else                r=mid;        }        printf("%.3f\n",l);    }    return 0;}
0 0
原创粉丝点击