POJ - 2728 Desert King【0/1分数规划】【最优比率生成树】

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.


题意

给定n个村庄的坐标,高度
要在n个村庄间修路,使得这n个村庄成为树形
每条路距离为连接的两个村庄坐标间的距离,成本为连接的两个村庄高度差
求使得总成本与总距离比率最小的方案


题目分析

0/1分数规划的经典模型

先预处理出每条路的距离 d i s 和成本 h i
每次二分时构建一个新图
每条边边权为 h i m i d d i s
然后在这张图上求最大生成树
若最大生成树总权值大于0,则令 L = m i d ,否则 R = m i d

因为是完全图
所以要用prim,而且不能堆优化


#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef double dd;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=1010;
int n,cnt; dd mid;
struct node{int x,y,z;}vil[maxn];
struct edge{dd dis,hi;}E[maxn][maxn];
priority_queue< pair<dd,int> > q;
dd d[maxn];
int vis[maxn];

dd calc(int u,int v)
{
    int x1=vil[u].x,y1=vil[u].y;
    int x2=vil[v].x,y2=vil[v].y;
    int tx=x1-x2; tx*=tx;
    int ty=y1-y2; ty*=ty;
    return sqrt((dd)(tx+ty));
}

int check()
{
    dd sum=0;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;++i)d[i]=1e9; d[1]=0;

    for(int i=1;i<=n;++i)
    {
        int u=-1; dd minn=1e9;
        for(int j=1;j<=n;++j)
        if(!vis[j]&&d[j]<minn)
        u=j,minn=d[j];

        vis[u]=1;
        for(int v=1;v<=n;++v)
        {
            if(!vis[v]&&E[u][v].hi-mid*E[u][v].dis<d[v])//注意这里的边权
            d[v]=E[u][v].hi-mid*E[u][v].dis;
        }
    }

    for(int i=2;i<=n;++i) sum+=d[i];
    return sum>=0;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break; cnt=0;
        for(int i=1;i<=n;++i)
        {
            int x=read(),y=read(),z=read();
            vil[i].x=x; vil[i].y=y; vil[i].z=z;
            for(int j=1;j<i;++j)//预处理每条边的距离、成本
            {
                dd dis=calc(i,j),hi=(dd)(abs(vil[i].z-vil[j].z));
                E[i][j].dis=E[j][i].dis=dis;
                E[i][j].hi=E[j][i].hi=hi;
            }
        }

        dd L=0,R=1000;
        while(R-L>1e-5)
        {
            mid=(L+R)/2;
            if(check())L=mid;
            else R=mid;
        }
        printf("%.3lf\n",L);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/80931265
今日推荐