zoukankan      html  css  js  c++  java
  • poj 2728 Desert King

    Desert King
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 27012   Accepted: 7501

    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

    题解:01分数规划
    重新定义边权,发现krusal过不去,现去学prim...
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define maxn 1009
    #define inf 123456789.
    using namespace std;
    
    int n;
    int x[maxn],y[maxn],h[maxn],vis[maxn];
    double v[maxn][maxn],dis[maxn][maxn],d[maxn];
    double l,r,mid,eps=1e-7;
    
    double muld(int i,int j){
        return (double)sqrt(1.*(x[i]-x[j])*(x[i]-x[j])+1.*(y[i]-y[j])*(y[i]-y[j]));
    }
    
    int mulv(int i,int j){
        return abs(h[i]-h[j]);
    }
    
    double prim(double p){
        memset(vis,0,sizeof(vis));
        d[1]=0;vis[1]=1;double sum=0.;
        for(int i=2;i<=n;i++)d[i]=1.*v[1][i]-p*dis[1][i];
        for(int i=2;i<=n;i++){
            double minn=inf;int h=0;
            for(int j=2;j<=n;j++)if(!vis[j]&&minn>d[j])minn=d[h=j];
            sum+=minn;d[h]=0;vis[h]=1;
            for(int j=2;j<=n;j++)if(!vis[j]&&1.*v[h][j]-p*dis[h][j]<d[j])d[j]=1.*v[h][j]-p*dis[h][j];
        }
        return sum;
    }
    
    int main(){
        while(1){
            scanf("%d",&n);
            if(!n)break;
            for(int i=1;i<=n;i++)scanf("%d%d%d",&x[i],&y[i],&h[i]);
            for(int i=1;i<=n;i++)
             for(int j=i+1;j<=n;j++){
                 dis[i][j]=dis[j][i]=muld(i,j);v[i][j]=v[j][i]=mulv(i,j);
             }
            l=0.;r=100.;
            while(r-l>eps){
                mid=(l+r)/2.;
                if(prim(mid)>0)l=mid;
                else r=mid;
            }  
            printf("%.3f
    ",r);
        }
        return 0;
    }
    
    
    



  • 相关阅读:
    OSPF综合实验一
    OSPF—开放最短路径优先协议详解---附:OSPF LSA 详解
    OSPF--LSA详解
    smfony设置量表之间的关系
    smyfony2 增删改查
    js中substring和substr的用法
    jQuery id模糊 选择器 批量处理
    《内存数据库和mysql的同步机制》
    linux 基本。。
    Django之模板引擎(母版)
  • 原文地址:https://www.cnblogs.com/zzyh/p/7640099.html
Copyright © 2011-2022 走看看