zoukankan      html  css  js  c++  java
  • Desert King

     

     Desert King 
    Time limit    3000 ms  
    Memory limit  65536 kB
     
    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

    题意:给你每个村庄的位置和高度,村与村的距离为点与点之间的距离,代价为两点之间的高度差,让你修n-1根水管,使得n个村庄全通水,在此基础上使得花费和/距离和最小!
    分析:很明显的最小生成树!但是我们平时求的最小生成树都是单纯的求距离和最小,这里求
    花费和/距离和最小,用二分,01分数规划最优比率生成树!
    x[i]为0或者1,代表边e[i]是否属于生成树,满足条件的R=(cost[i]*x[i])/(dis[i]*x[i]),转化为Z=(cost[i]-dis[i]*r)*x[i],求最小值,用prim算法跑一下!

    AC代码:
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define INF 1e9
    #define M 100
    #define N 1010
    using namespace std;
    typedef long long ll;
    int n;
    double dis[N][N],h[N][N],d[N][N],lowcost[N],vis[N];
    struct point{
        double x,y,z;
    }a[N];
    double A(int i,int j){
        return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
    }
    double prim(double num){
        //memset(lowcost,0,sizeof(lowcost));
        memset(vis,0,sizeof(vis));
        for (int i=0;i<n;i++)
           for (int j=i+1;j<n;j++)
             d[j][i]=d[i][j]=h[i][j]-num*dis[i][j];
        int k;
        double mini,sum=0;
        for (int i=0;i<n;i++)
            lowcost[i]=d[0][i];
            vis[0]=1;
            for (int i=1;i<n;i++){
                mini=INF;
                k=-1;
                for (int j=0;j<n;j++)
                    if (!vis[j]&&lowcost[j]<mini)
                        mini=lowcost[j],k=j;
                if (k==-1) break;
                sum+=lowcost[k];
                vis[k]=1;
                for (int j=0;j<n;j++)
                    if (!vis[j]&&lowcost[j]>d[k][j])
                        lowcost[j]=d[k][j];
            }
        return sum>=0;
    }
    int main(){
       ios::sync_with_stdio(false);
       while(~scanf("%d",&n)&&n!=0){
           memset(dis,0,sizeof(dis));
           memset(h,0,sizeof(h));
           for (int i=0;i<n;i++)
            scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z);
           for (int i=0;i<n;i++)
              for (int j=i+1;j<n;j++)
                dis[j][i]=dis[i][j]=A(i,j),h[j][i]=h[i][j]=abs(a[i].z-a[j].z);
           double l=0,r=100.0;
        while (r-l>=0.000001){
           double mid=l+(r-l)/2.0;
           if (prim(mid))  l=mid;
           else  r=mid;
         }
          printf("%.3lf
    ",r);
      }
       return 0;
    }
     
     
  • 相关阅读:
    判断开始时间不能小于结束时间
    angular1.0使用echarts点刷新再次调用echarts方法
    解决angular4.0打包后不能再继续打包
    html拼接+layer按钮
    angular.js+echarts
    nginx配置
    NET_Framework_4.0installer.rar
    IIS6.0开启gzip压缩
    IIS的应用程序池优化方法
    远程桌面连接电脑后键盘失灵解决
  • 原文地址:https://www.cnblogs.com/lisijie/p/8419753.html
Copyright © 2011-2022 走看看