POJ-2728 Desert King 01分數規劃 二分/迭代

題目鏈接: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

costxirdisxi=0

設一個函數,自變量爲r值,
f(r)=costxirdisxi

觀察這個函數,假如{xi}固定,則這個函數就是座標系中一條直線(y=B−A⋅x),每一組{xi}對應着一條直線,這些直線斜率非正(因爲−A=−∑dis⋅xi≤0),縱截距非負(因爲B=∑cost⋅xi≥0 ),如圖1。
這裏寫圖片描述
思路:這道題的最終答案可以變形成f(r)=costxirdisxi 的函數,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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章