zoukankan      html  css  js  c++  java
  • POJ2728 Desert King 【最优比率生成树】

    POJ2728 Desert King


    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


    已经确定了一个根,再选择一种优美的结构,不就是树吗(结构是树的时候代价最小),那么这道题就变成一道最优比率生成树,这个题目j就是要求Min(/),套一个板子就好啦


    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    using namespace std;
    const int N=1010;
    const double eps=1e-5;
    const double INF=1e16;
    int n;
    double f[N][N],dis[N][N],x[N],y[N],z[N],minv[N];
    int vis[N];
    double getdis(int a,int b){
        return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
    }
    int check(double x){
        memset(vis,0,sizeof(vis));
        double sum=0;vis[1]=1;
        for(int i=1;i<=n;i++)minv[i]=f[1][i]-x*dis[1][i];
        for(int i=2;i<=n;i++){
            double tmp=INF;int k=-1;
            for(int j=2;j<=n;j++)
                if(!vis[j]&&minv[j]<tmp)
                    k=j,tmp=minv[j];
            if(k==-1)break;
            vis[k]=1;
            sum+=tmp;
            for(int j=2;j<=n;j++)
                if(!vis[j]&&f[k][j]-x*dis[k][j]<minv[j])
                    minv[j]=f[k][j]-x*dis[k][j];
        }
        return sum>=0;
    }
    int main(){
        while(1){
            scanf("%d",&n);
            if(!n)break;
            for(int i=1;i<=n;i++)
                scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
            for(int i=1;i<=n;i++)
                for(int j=i+1;j<=n;j++){
                    dis[i][j]=dis[j][i]=getdis(i,j);
                    f[i][j]=f[j][i]=abs(z[i]-z[j]);
                }
            double l=0.0,r=100.0;
            while(r-l>=eps){
                double mid=(l+r)/2;
                if(check(mid))l=mid;
                else r=mid;
            }
            printf("%.3lf
    ",r);
        }
        return 0;
    }
  • 相关阅读:
    岩石圈
    地球及其圈层结构
    如何请教一个技术问题
    中国游戏路在何方?
    5.4删除二叉搜索树的任意元素
    5.3 删除二叉搜索树的最大元素和最小元素
    uni-app开发小程序准备阶段
    5.2二叉搜索树遍历(前序、中序、后序、层次、广度优先遍历)
    5.1二叉搜索树基础
    【loj
  • 原文地址:https://www.cnblogs.com/dream-maker-yk/p/9676406.html
Copyright © 2011-2022 走看看