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
  • 相关阅读:
    div 圆角
    CSS定义鼠标经过时鼠标图型样式
    如何判断浏览器类型然后让它读取指定的CSS
    如何分别指定ie6及ie7浏览器的css
    用CSS控制DIV居中失效的解决方法
    css如何控制文字多行显示,溢出截断后末尾出现省略...
    样式命名规则
    type="file" 谁用过这个属性给定义样式
    有利于SEO的DIV+CSS的命名规矩小结
    左右两个div高度自动一致,自适应高度
  • 原文地址:https://www.cnblogs.com/plysc/p/11419560.html
Copyright © 2011-2022 走看看