Poj 2728 Desert King【最优比例生成树/01分数规划】

Desert King
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 25612 Accepted: 7110

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


题目大意:

让你构建出一棵树,两点相连的花费是高度差,长度是坐标距离,让你求最小单位长度花费比例。


思路:


1、很显然问题属于01分数规划问题。

当有求:Σ(a【i】)/Σ(b【i】)的最小(大)值的时候,我们可以将问题转化变成减法:

设定函数F(L)=Σ(a【i】)-L*Σ(b【i】);

若此时F(L)<0,那么肯定有Σ(a【i】)-L*Σ(b【i】)<0,那么就有:Σ(a【i】)/Σ(b【i】)<L;

根据化出的等式可以得知,我们有比L更小的结果。那么我们这里可以二分枚举这个L,同时也就是在二分枚举最终的答案。

若有F(L)<0,我们可以继续减小答案,反之我们要增大答案。


2、那么对于我们二分过程的F(L)的计算,我们只要跑一遍最小生成树算法即可啊。

又因为是完全图,边数比较多,所以我们这里使用Prim算法。


#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
struct node
{
    int x,y,z;
}a[1500];
int n;
int vis[1002];
double dis[1002];
double map[1002][1002];
double cost[1002][1002];
double diss(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int Slove(double mid)
{
    double sum=0;
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    for(int i=2;i<=n;i++)
    {
        dis[i]=cost[1][i]-mid*map[1][i];
    }
    vis[1]=1;
    for(int i=2;i<=n;i++)
    {
        double minn=10000000000;
        int k=-1;
        for(int j=1;j<=n;j++)
        {
            if(vis[j]==0&&dis[j]<minn)
            {
                minn=dis[j];
                k=j;
            }
        }
        if(minn==10000000000)break;
        sum+=minn;
        vis[k]=1;
        for(int j=2;j<=n;j++)
        {
            if(vis[j]==0&&dis[j]>cost[k][j]-mid*map[k][j])dis[j]=cost[k][j]-mid*map[k][j];
        }
    }
    if(sum<0)return 1;
    else return 0;
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        for(int i=1;i<=n;i++)scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                map[i][j]=diss(a[i],a[j]);
                cost[i][j]=fabs(a[i].z-a[j].z);
            }
        }
        double ans=0;
        double l=0;
        double r=10005000.0;
        for(int i=0;i<50;i++)
        {
            double mid=(l+r)/2;
            if(Slove(mid))
            {
                ans=mid;
                r=mid;
            }
            else l=mid;
        }
        printf("%.3f\n",ans);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值