POJ 2728 Desert King (01分数规划)

DesertKing渠道最优构建
本文探讨了在沙漠国家中如何构建最优渠道网络的问题。通过使用01分数规划算法,实现了渠道成本与长度比最小化的目标,确保每个村庄都能通过唯一的渠道连接到首都。
                Desert King
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions:29775 Accepted: 8192

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分数规划算法,在写一遍复习这个算法吧,就是用一个估计值,乘上物品的权值,在于物品的价值作比较,这个比较的过程,不止一个节点的比较,所以要将物品的权值和价值累和,在进行比较,这里的权值和价值都没有再乘或除里面,所以这里可以将其累和。如果比较的结果,是价值大了,就调小x,否则,调大x;

除此之外,我要喷一下POJ,说好的数据范围1-1000,我的数组开了1024,tle了一晚上,之后改成了1066,就过了。。过了、、、过了。。。


代码
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define inf 1000000000
using namespace std;
int x[1066],y[1066],h[1066];
bool book[1066];
int n,t;
long double d[1066][1066],dis[1066],z[1066][1066],w[1066][1066];;
long double Abs(long double x){return (x>=0)?x:-x;}
bool prim(double x)
{
    for(int i=1;i<=n;i++){
        dis[i]=inf;
        book[i]=false;
        for(int j=i+1;j<=n;j++){
            d[i][j]=d[j][i]=z[i][j]-x*w[i][j];
        }
    }
    dis[1]=0;
    dis[0]=inf;
    long double ans=0;
    for(int j=1;j<=n;j++){
        int t=0;
        for(int i=1;i<=n;i++){
            if(!book[i]&&dis[t]>dis[i]){t=i;}
        }
        ans+=dis[t];book[t]=true;
        for(int i=1;i<=n;i++){
            if(!book[i]&&d[t][i]<dis[i]){
                dis[i]=d[t][i];
            }
        }

    }
    return ans>0;
}

int main()
{
    while(scanf("%d",&n)!=EOF&&n){
        for(int i=1;i<=n;i++){
            scanf("%d%d%d",&x[i],&y[i],&h[i]);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                w[i][j]=sqrt((long double)((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
                z[i][j]=Abs(h[i]-h[j]);
            }
        }
        long double l=0,r=1000000000;
        while(r-l>1e-6){
            double mid=(l+r)/2;
            if(prim(mid)){l=mid;}
            else r=mid;
        }
        printf("%.3f\n",(double)l);
    }
}

 

 

转载于:https://www.cnblogs.com/ZGQblogs/p/9383869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值