POJ-2728 Desert King 01分数规划 二分/迭代

119 篇文章 1 订阅
113 篇文章 1 订阅

题目链接:Desert King

Desert King
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 28809 Accepted: 7948
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

推荐一个详解博客:01分数规划问题
会的就不用看了。

二分法
设r最大值为r∗,

r=costxidisxi r ∗ = ∑ c o s t ⋅ x i ∑ d i s ⋅ x i

costxirdisxi=0 ∑ c o s t ⋅ x i − r ∗ ⋅ ∑ d i s ⋅ x i = 0

设一个函数,自变量为r值,
f(r)=costxirdisxi f ( r ) = ∑ c o s t ⋅ x i − r ∗ ⋅ ∑ d i s ⋅ x i

观察这个函数,假如{xi}固定,则这个函数就是坐标系中一条直线(y=B−A⋅x),每一组{xi}对应着一条直线,这些直线斜率非正(因为−A=−∑dis⋅xi≤0),纵截距非负(因为B=∑cost⋅xi≥0 ),如图1。
这里写图片描述
思路:这道题的最终答案可以变形成 f(r)=costxirdisxi f ( r ) = ∑ c o s t ⋅ x i − r ∗ ⋅ ∑ d i s ⋅ x i 的函数,xi(取值0和1)代表是否选取这条边,图中的每一条函数,代表f(r)的所有情况,因此答案就是与x轴交点中,x值最小的(可以好好想想),我们可以二分x值,来找答案,而判断可以用最小生成树来判断。

下面是代码(我认为这个写的很好,就粘贴上去,有二分的写法和迭代的写法):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define nMax 1050
#define inf 0x7fffffff
static double eps = 1e-4;
int vis[nMax],x[nMax],y[nMax],z[nMax],pre[nMax];
double dis[nMax],cost[nMax][nMax],dist[nMax][nMax];
int n;
double prim(double x)//普利姆算法求最小生成树
{
    double totalcost = 0, totaldist = 0;
    double sum = 0.0;
    for (int i = 1; i <= n; ++ i)
    {
        pre[i] = 1;
    }
    dis[1] = 0;
    memset(vis, 0, sizeof(vis));
    vis[1] = 1;
    for (int i = 2; i <= n; ++ i)
    {
        dis[i] = cost[1][i] - dist[1][i] * x;
    }
    int k;
    for (int i = 2; i <= n; ++ i)
    {
        double minCost = inf;
        for (int j = 2; j <= n; ++ j)
        {
            if (!vis[j] && dis[j] < minCost)
            {
                minCost = dis[j];
                k = j;
            }
        }
        vis[k] = 1;
        sum += minCost;//for 二分
        totalcost += cost[pre[k]][k];
        totaldist += dist[pre[k]][k];
        for (int j = 1; j <= n; ++ j)
        {
            if (!vis[j] && dis[j] > cost[k][j] - dist[k][j] * x)
            {
                dis[j] = cost[k][j] - dist[k][j] * x;
                pre[j] = k;
            }
        }
    }
#if 0//0 for 二分, 1 for 迭代
    return totalcost / totaldist;
#else
    return sum;
#endif

}
int main()
{
    while (scanf("%d", &n), n)
    {
        for (int i = 1; i <= n; ++ i)
        {
            scanf("%d%d%d", &x[i], &y[i], &z[i]);
            for (int j = 1; j < i; ++ j)
            {
                double tmp = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
                cost[i][j] = cost[j][i] = abs(z[i] - z[j]);//海拔
                dist[i][j] = dist[j][i] = sqrt(tmp);//欧式距离
            }
        }
        double a = 0;
#if 0//1为迭代,0为二分
        while (1)//迭代求最大值
        {
            double b = prim(a);
            if (abs(a - b) < eps)
            {
                printf("%.3f\n", a);
                break;
            }
            else
                a = b;
        }
#else
        double head = 0,tail = 100000.0;
        while (tail - head > 1e-5)
        {
            double mid = (head + tail) / 2.0;
            a = prim(mid);
            if (a >= 0)
            {
                head = mid;
            }
            else
                tail = mid;
        }
        printf("%.3f\n", tail);
#endif
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值