zoukankan      html  css  js  c++  java
  • poj 2728 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

    01分数规划
    最小生成树用prim

    #include<iostream>
    #include<cmath>
    #include<cstring>
    using namespace std;
    
    const int maxn = 1e3+10;
    
    const double eps = 1e-7;
    
    int n,x[maxn],y[maxn],z[maxn],cost[maxn][maxn];
    
    double len[maxn][maxn],mp[maxn][maxn],maxx,dis[maxn];
    
    bool vis[maxn];
    
    double getdis(int i,int j){
        return sqrt((double)(x[i]-x[j])*(x[i]-x[j])+(double)(y[i]-y[j])*(y[i]-y[j]));
    }
    
    void build(){
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++){
                cost[i][j]=cost[j][i]=abs(z[i]-z[j]);
                len[i][j]=len[j][i]=getdis(i,j);maxx=max(maxx,cost[i][j]/len[i][j]);
            }
    }
    
    double prim(){
        for(int i=1;i<=n;i++) dis[i]=mp[1][i];
        memset(vis,0,sizeof(vis));
        vis[1]=1;double ret=0;
        for(int i=2;i<=n;i++){
            double minn=10000000000;
            int pos;
            for(int j=1;j<=n;j++){
                if(!vis[j]&&minn>dis[j]) minn=dis[j],pos=j;
            }
            ret+=dis[pos];vis[pos]=1;
            for(int j=1;j<=n;j++){
                if(!vis[j]&&dis[j]>mp[pos][j]) dis[j]=mp[pos][j];
            }
        }
        return ret;
    }
    
    bool check(double mid){
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++) mp[i][j]=mp[j][i]=cost[i][j]-mid*len[i][j];
            return prim()<0;
    }
    
    int main(){
        while(scanf("%d",&n)&&n){
            maxx=0;
            for(int i=1;i<=n;i++) scanf("%d%d%d",&x[i],&y[i],&z[i]);
            build();
            double l=0,r=maxx;
            while(r-l>eps){
                double mid=(l+r)/2;
                if(check(mid)) r=mid;
                else l=mid;
                
            }
            printf("%.3f
    ",l);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Ui设计哪里有好的素材
    android重写view和viewgroup的区别
    笔记本电脑连接wifi有时候会自动断网提示有限的访问权限解决办法
    ADT 怎么删除logcat过滤规则
    Android开发在使用第三方推送的时候出现INSTALL_FAILED_VERSION_DOWNGRADE
    评论的延迟加载(转载)
    让Entity Framework支持MySql数据库(转载)
    Net4.0---AspNet中URL重写的改进(转载)
    Entity Framework 使用
    深入浅出JSONP--解决ajax跨域问题
  • 原文地址:https://www.cnblogs.com/plysc/p/11419560.html
Copyright © 2011-2022 走看看