Desert King (poj 2728 最优比率生成树 0-1分数规划)


Language:
Default

Desert King

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 22113   Accepted: 6187

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

Beijing 2005

题意:将n个村庄连在一起,告诉每个村庄的三维坐标,村庄之间的距离为水平方向上的距离,花费为垂直方向上的高度差,求把村庄连接起来的最小的花费与长度之比为多少。

思路:经典的01分数规划问题,参考这位大神的讲解应该就能明白了:http://www.cnblogs.com/Fatedayt/archive/2012/03/05/2380888.html

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 2005;
const int MAXM = 200010;
const int N = 1005;

double x[maxn],y[maxn],z[maxn];
double dist[maxn],mp[maxn][maxn],len[maxn][maxn],cost[maxn][maxn];
bool vis[maxn];
int pre[maxn];
int n;

double Dis(int i,int j)
{
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}

double prim(double r)
{
    int i,j,now;
    double mi,c=0,l=0;
    for (i=0;i<n;i++)
    {
        dist[i]=INF;
        for (j=0;j<n;j++)
        {
            mp[i][j]=cost[i][j]-r*len[i][j];
        }
    }
    for (i=0;i<n;i++)
    {
        dist[i]=mp[i][0];
        pre[i]=0;
        vis[i]=false;
    }
    dist[0]=0;
    vis[0]=true;
    for (i=1;i<n;i++)
    {
        mi=INF;now=-1;
        for (j=0;j<n;j++)
        {
            if (!vis[j]&&mi>dist[j])
            {
                mi=dist[j];
                now=j;
            }
        }
        if (now==-1) break;
        vis[now]=true;
        c+=cost[pre[now]][now];
        l+=len[pre[now]][now];
        for (j=0;j<n;j++)
        {
            if (!vis[j]&&dist[j]>mp[now][j])
            {
                dist[j]=mp[now][j];
                pre[j]=now;
            }
        }
    }
    return c/l;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j;
    while (sf(n))
    {
        if (n==0) break;
        for (i=0;i<n;i++)
            scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
        for (i=0;i<n;i++)
        {
            for (j=0;j<n;j++)
            {
                len[i][j]=Dis(i,j);
                cost[i][j]=fabs(z[i]-z[j]);
            }
        }
        double r=0,rate;    //r迭代初值为0
        while (1)
        {
            rate=r;
            r=prim(r);
            if (fabs(r-rate)<eps) break;
        }
        printf("%.3f\n",r);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-05 08:18:13

Desert King (poj 2728 最优比率生成树 0-1分数规划)的相关文章

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

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

poj 2718 最优比例生成树(01分数规划)模板

/* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1100 #define eps 1e-10 #define inf 0x3fffffff struct node { int u,v,w; }p[N]; double ma[N][N]; double distance(int i,int j) { return sqrt(1.0*(p[i].u-p[j].u

POJ 2728 最优比率生成树

思路: 转自魏神: 其实原题就是求 MIN( ∑CiXi / ∑DiXi ) Xi∈{0,1} ,对每个生成树,设其比率r=∑CiXi / ∑DiXi ,可得∑CiXi - ∑DiXi * r=0(条件1) 那么对于所有的生成树,显然∑CiXi - ∑DiXi * min(r) >= 0,当 ∑CiXi / ∑DiXi = min(r)时,等号成立. 而我们现在不知道min(r)是多少,只好进行枚举,对每个枚举的r ,构建新的权值(Ci-Di*r),然后求最小生成树,  为什么求最小呢? 我的理

【POJ】【2728】 Desert King 最优比率生成树

题意:给出每个点的坐标(x,y,z),两点间距离是x,y的直线距离,边权为z差,求∑边权 / ∑距离 的最小值. 最优比率生成树!(分数规划) 就是根据分数规划的思想建树,每次看得到的总和是正是负. 二分代码: #include<string.h> #include<stdio.h> #include<stdlib.h> #include<math.h> #define N 1010 typedef struct KSD { int x,y,z; }ksd;

[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(初遇最优比率生成树)

题目链接:http://poj.org/problem?id=2728 题意:给出几个村庄的坐标x[i]和y[i],以及海拔z[i].要在这些村庄之间建水渠,费用和两个村庄的海拔差成正比,水渠长度和村庄二维坐标(x,y)上的距离成正比,要求一种方案使得(总的花费/总的水渠长度)最小,输出这个最小值,保留三位小数. 这是一道0,1分数规划的题目,求的是一棵生成树sigma(dh)/sigma(l) 的最小值(dh是树上两点之间的高度差,l是树上两点之间的边长). 按0,1分数规划的思路对解进行分析

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>

【最优比率生成树】poj2728 Desert King

最优比率生成树教程见http://blog.csdn.net/sdj222555/article/details/7490797 个人觉得很明白易懂,但他写的代码略囧. 模板题,但是必须Prim,不能用Kruscal,因为是完全图 Code: 1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struc