POJ 2728 Desert King (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

时间: 2024-10-08 01:51:06

POJ 2728 Desert King (01分数规划)的相关文章

POJ 2728 Desert King 0-1分数规划

题意:链接 方法: 0-1分数规划 解析: 这是之前没填的坑,现在来填坑. 这道题题意就是n个三维坐标系的点,任意两点都可以连边,每条边的花费是两点在xOy坐标系下的欧几里得距离,每条边的收益是两点的z值差的绝对值. n个点连成一棵树 求最小的花费比收益. 即求最大的收益比花费. 一看求的东西就可以考虑0-1分数规划咯? 所以二分那个L,然后每条边的get-L*cost就是每条边新的权值,因为要拿最大的n-1条,所以上MST,但是这题是n^2的边,kruscal的话是n^2logn^2*log(

$POJ$2728 $Desert\ King$ 01分数规划

正解:01分数规划 解题报告: 传送门! 感觉挺板子的,,, 因为还没做,先瞎口胡一个做法$QAQ$ 如果翻车了请当做没看见我如果错了等$get$正确做法会重新编辑的$QAQ$ 就因为有$n\leq 100$,于是如果把它联成一个完全图边数连$1e4$都没有 所以考虑直接连成一个完全图,就成了之前寒假考试题二分专题那次的$T1$辽,就直接无脑01分数规划+最大生成树就欧克 $over$? 原文地址:https://www.cnblogs.com/lqsukida/p/10827485.html

poj2728 Desert King --- 01分数规划 二分水果。。

这题数据量较大,普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; #define N 1010 double mp[N][N],c[N][N],x

poj2728 Desert King——01分数规划

题目:http://poj.org/problem?id=2728 第一道01分数规划题!(其实也蛮简单的) 这题也可以用迭代做(但是不会),这里用了二分: 由于比较裸,不作过多说明了. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #define eps 1e-6 using namespace std; int const inf=0x3f3f3f;

poj 2728 Desert King(最优比率生成树,01分数规划)

http://poj.org/problem?id=2728 大致题意:有n个村庄,输入每个村庄的位置和高度,这n个村庄要连在一起,村与村之间的长度为他们之间的欧几里得距离,花费是两村之间的高度差,要求连在一起的花费和与距离和之比的最小值. 思路:明显的最优比率生成树.二分答案λ,每条边重新赋权c[i] - λd[i] ,因为要求比值最小,那么对于所有的生成树,它们的f[λ]必须>=0,所以只需求得基于最小生成树的f'[λ],当f'[λ] = 0时即找到了正解λ*. 二分: #include <

poj 2728 Desert King(最小比率生成树,迭代法)

引用别人的解释: 题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求方案使得费用与距离的比值最小 很显然,这个题目是要求一棵最优比率生成树, 概念 有带权图G, 对于图中每条边e[i], 都有benifit[i](收入)和cost[i](花费), 我们要求的是一棵生成树T, 它使得 ∑(benifit[i]) / ∑(cost[i]), i∈T 最大(或最小). 这

[POJ 2728]Desert King(0-1分数规划/最优比率生成树)

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

POJ 2728 Desert King (最优比率生成树---01分数规划)

题目地址:POJ 2728 01分数规划的应用之一-最优比率生成树. 跟普通的01分数规划类似,只是这题的验证函数改成了最小生成树来验证.弱用的迭代法. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map>

POJ 2728 Desert King(最优比率生成树 01分数规划)

http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简单,因为这题是稠密图,所以用prim算法会好点. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector>