zoukankan      html  css  js  c++  java
  • POJ 2728 Desert King(最优比例生成树 二分 | Dinkelbach迭代法)

    Desert King
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 25310   Accepted: 7022

    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

    题目链接:POJ 2728

    原理与NYOJ那道题相似,只是这次的X集合取值有了更多的限制,即取X的边必须要是一颗生成树。

    题目要求的是令$ {Sigma cost} over {Sigma len} $最小,这是显然的,日常生活中肯定是让平均花费越小才越省钱,类比NYOJ的入门题,如何确定${X_i}$的取值呢?不是简单地贪心找最大,而是在最小(最大)生成树中确定它是要找一个最大的比例,而且这题是要找到一个最小的比例,那么我们用最小生成树来做即可。二分的速度没有那种迭代法快,有时候还容易超时……,哦对了这题我用邻接表手写的200W堆和自带的pq都是超时的,不得已去搬了一个邻接矩阵的朴素Prim模版没想到过了,可能是我堆写的丑……

    二分代码(2360MS):

    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <sstream>
    #include <numeric>
    #include <cstring>
    #include <bitset>
    #include <string>
    #include <deque>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    #define CLR(arr,val) memset(arr,val,sizeof(arr))
    #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
    typedef pair<int, int> pii;
    typedef long long LL;
    const double PI = acos(-1.0);
    const int N = 1010;
    const int M = N * N * 2;
    const double eps = 1e-6;
    struct info
    {
        double x, y, z;
    } P[N];
    
    bitset<N>vis;
    double cost[N][N], len[N][N], lowcost[N], Map[N][N];
    
    inline double getlen(info a, info b)
    {
        return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
    inline double getcost(info a, info b)
    {
        return fabs(a.z - b.z);
    }
    double prim(int n, double k)
    {
        int i, j;
        vis.reset();
        for (i = 1; i <= n; ++i)
            for (j = 1; j <= n; ++j)
                Map[i][j] = cost[i][j] - k * len[i][j];
    
        for (i = 1; i <= n; ++i)
            lowcost[i] = Map[1][i];
    
        double ret = 0;
        vis[1] = 1;
        lowcost[1] = 1e16;
    
        for (i = 1; i < n; ++i)
        {
            double Min = 1e16;
            int pos;
            for (j = 1; j <= n; ++j)
            {
                if (!vis[j] && lowcost[j] < Min)
                {
                    Min = lowcost[j];
                    pos = j;
                }
            }
            ret += lowcost[pos];
            vis[pos] = 1;
            for (j = 1; j <= n; ++j)
            {
                if (!vis[j] && lowcost[j] > Map[pos][j])
                    lowcost[j] = Map[pos][j];
            }
        }
        return ret;
    }
    int main(void)
    {
        int n, i, j;
        while (~scanf("%d", &n) && n)
        {
            for (i = 1; i <= n; ++i)
                scanf("%lf%lf%lf", &P[i].x, &P[i].y, &P[i].z);
            double Maxk = -1e16;
            for (i = 1; i <= n; ++i)
            {
                for (j = i + 1; j <= n; ++j)
                {
                    double c = getcost(P[i], P[j]);
                    double l = getlen(P[i], P[j]);
                    cost[i][j] = cost[j][i] = c;
                    len[i][j] = len[j][i] = l;
                    Maxk = max(Maxk, c / l);
                }
            }
            double L = 0, R = Maxk;
            double ans = 0;
            while (fabs(R - L) >= eps)
            {
                double mid = (L + R) / 2.0;
                if (prim(n, mid) > 0)
                {
                    ans = mid;
                    L = mid;
                }
                else
                    R = mid;
            }
            printf("%.3f
    ", ans);
        }
        return 0;
    }

     

    Dinkelbach迭代法代码:

    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <sstream>
    #include <numeric>
    #include <cstring>
    #include <bitset>
    #include <string>
    #include <deque>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    #define CLR(arr,val) memset(arr,val,sizeof(arr))
    #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
    typedef pair<int, int> pii;
    typedef long long LL;
    const double PI = acos(-1.0);
    const int N = 1010;
    const double eps = 1e-6;
    struct info
    {
        double x, y, z;
    } P[N];
    
    bitset<N>vis;
    double cost[N][N], len[N][N], lowcost[N], Map[N][N];
    int pre[N];
    
    inline double getlen(info a, info b)
    {
        return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
    inline double getcost(info a, info b)
    {
        return fabs(a.z - b.z);
    }
    double prim(int n, double k)
    {
        /**< 用k值重新赋边权值 */
        int i, j;
        for (i = 1; i <= n; ++i)
            for (j = 1; j <= n; ++j)
                Map[i][j] = cost[i][j] - k * len[i][j];
    
        /**< 必要的初始化 */
        vis.reset();
        for (i = 1; i <= n; ++i)
        {
            lowcost[i] = Map[1][i];
            pre[i] = 1;
        }
        vis[1] = 1;
        double sumcost = 0, sumlen = 0;
    
        /**< Prim过程 */
        for (i = 1; i < n; ++i)
        {
            double Min = 1e19;
            int pos = 0;
            for (j = 1; j <= n; ++j)
            {
                if (!vis[j] && lowcost[j] < Min)
                {
                    pos = j;
                    Min = lowcost[j];
                }
            }
            vis[pos] = 1;
            sumcost += cost[pre[pos]][pos];
            sumlen += len[pre[pos]][pos];
            for (j = 1; j <= n; ++j)
            {
                if (!vis[j] && lowcost[j] > Map[pos][j])
                {
                    lowcost[j] = Map[pos][j];
                    pre[j] = pos;
                }
            }
        }
        return sumcost / sumlen;
    }
    int main(void)
    {
        int n, i, j;
        while (~scanf("%d", &n) && n)
        {
            for (i = 1; i <= n; ++i)
                scanf("%lf%lf%lf", &P[i].x, &P[i].y, &P[i].z);
            for (i = 1; i <= n; ++i)
            {
                for (j = i + 1; j <= n; ++j)
                {
                    double c = getcost(P[i], P[j]);
                    double l = getlen(P[i], P[j]);
                    cost[i][j] = cost[j][i] = c;
                    len[i][j] = len[j][i] = l;
                }
            }
            double ans = 0, temp = 0;
            while (1)
            {
                temp = prim(n, ans);
                if (fabs(ans - temp) < eps)
                    break;
                ans = temp;
            }
            printf("%.3f
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    Python 3: 加密简介
    5 个最受人喜爱的开源 Django 包
    ROS tf-增加坐标系
    ROS tf监听编写
    ROS tf广播编写
    ROS tf基础使用知识
    ROS tf-数据类型
    Windows Vistual Studio 2013/2015 MRPT安装
    OMPL 在windows下的安装
    CMake 设置Target输出目录和后缀名
  • 原文地址:https://www.cnblogs.com/Blackops/p/6548986.html
Copyright © 2011-2022 走看看