zoukankan      html  css  js  c++  java
  • poj 2728 Desert King (最优比率生成树)

    Desert King
    Time Limit: 3000MS   Memory Limit: 65536K
         

    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

     
    题目大意:构建最优比率生成树
    每条边的花费=连接两点的高度差,距离=平面两点间距离(官方名称:欧几里得距离)
    最小化 ∑ 每条边的花费/距离
    01分数规划+prim
    prim构造最小生成树的标准是 w=这条边的花费-这条边的距离*二分的mid
    最后判断选用边的w是否大于0
    01分数规划就用在这里
    然而初学,并没有想到,一直在思考怎么在prim过程中套01规划
    再就是判断式子>0,移动下界,否则移动上界
    这是固定的,与最终求最大最小值没有关系
    受了刚做的一道01规划题影响http://www.cnblogs.com/TheRoadToTheGold/p/6546981.html
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath> 
    #define eps 1e-6
    using namespace std;
    int n;
    double l,r,mid,ans,p;
    double x[1001],y[1001],h[1001],dis[1001][1001],cost[1001][1001],minn[1001],w[1001][1001];
    bool v[1001];
    bool check(double k)
    {
        p=0;
        memset(v,0,sizeof(v));
        for(int i=1;i<=n;i++) minn[i]=w[1][i];
        minn[1]=0;v[1]=true;
        int s=n-1;
        while(s--)
        {
            int point;double d=10000010;
            for(int i=1;i<=n;i++)
             if(!v[i]&&minn[i]<d)
             {
                point=i;d=minn[i];
             }
            p+=d;v[point]=true;
            for(int i=1;i<=n;i++)
             if(!v[i]&&w[point][i]<minn[i])
              minn[i]=w[point][i];
        }
        return p>=0;
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            if(!n) return 0;
            for(int i=1;i<=n;i++) scanf("%lf%lf%lf",&x[i],&y[i],&h[i]);
            for(int i=1;i<n;i++)
                 for(int j=i+1;j<=n;j++)
                  {
                       dis[i][j]=sqrt(pow(abs(x[i]-x[j]),2)+pow(abs(y[i]-y[j]),2));
                        cost[i][j]=abs(h[i]-h[j]);
                  }
            l=0,r=1000;ans=0;
            while(fabs(l-r)>eps)
            {
                mid=(l+r)/2;
                for(int i=1;i<n;i++)
                 for(int j=i+1;j<=n;j++)
                  w[i][j]=w[j][i]=cost[i][j]-mid*dis[i][j];
                if(check(mid)) 
                {
                    ans=mid;
                    l=mid+eps;
                }
                else r=mid-eps;
            }
            printf("%.3lf
    ",ans);
        }
        
    }
  • 相关阅读:
    SQL中UNION的使用
    [转]身份证号准确性检测
    shell中if/seq/for/while/until
    shell中数字、字符串、文件比较测试
    shell简介及变量的定义查看撤销
    grep/字符/次数匹配/锚定符/小大括号/wc/tr/cut/sort/uniq
    linux全局和个人配置文件说明
    linux文件的3个时间和7种文件类型
    linux常用配置文件和命令总结
    目录方式扩展swap分区大小
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6547745.html
Copyright © 2011-2022 走看看