zoukankan      html  css  js  c++  java
  • (最优比例生成树) poj 2728

    Desert King
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 21855   Accepted: 6103

    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

    Source

     

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<set>
    #include<stack>
    #include<map>
    #define eps 1e-8
    #define INF 100000000
    using namespace std;
    int n;
    struct node
    {
        double x,y,z;
    }e[1010];
    double len[1010][1010],cost[1010][1010],mp[1010][1010],low[1010];
    bool vis[1010];
    double dis(node a,node b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    double prim()
    {
        memset(vis,0,sizeof(vis));
        double minn,ans=0;
        int pos;
        pos=1,vis[pos]=1;
        for(int i=1;i<=n;i++)
        {
            if(i!=pos)
                low[i]=mp[pos][i];
        }
        for(int i=1;i<n;i++)
        {
            minn=INF;
            for(int j=1;j<=n;j++)
            {
                if(!vis[j]&&low[j]<minn)
                {
                    pos=j;
                    minn=low[j];
                }
            }
            vis[pos]=1;
            ans+=minn;
            for(int j=1;j<=n;j++)
            {
                if(!vis[j]&&low[j]>mp[pos][j])
                    low[j]=mp[pos][j];
            }
        }
        return ans;
    }
    double solve(double mid)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                mp[i][j]=cost[i][j]-mid*len[i][j];
            }
        }
        return prim();
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            if(n==0)
                break;
            for(int i=1;i<=n;i++)
                scanf("%lf%lf%lf",&e[i].x,&e[i].y,&e[i].z);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    len[i][j]=dis(e[i],e[j]);
                    cost[i][j]=abs(e[i].z-e[j].z);
                }
            }
            for(int i=1;i<=n;i++)
                mp[i][i]=0;
            double l,r,mid;
            l=0,r=10000000;
            while(r-l>=eps)
            {
                mid=(l+r)*0.5;
                if(solve(mid)>eps)
                    l=mid;
                else
                    r=mid;
            }
            printf("%.3f
    ",r);
        }
        return 0;
    }
    

      

  • 相关阅读:
    java提高篇(四)-----抽象类与接口
    hdu1004----用java链表实现
    jkfladsdjfkldsa
    Handler_1
    Handler实现线程间的通信2
    Handler实现线程间的通信1
    Handler基本运行机制
    Android线程
    Activity声明周期2
    Activity声明周期1
  • 原文地址:https://www.cnblogs.com/water-full/p/4528016.html
Copyright © 2011-2022 走看看