zoukankan      html  css  js  c++  java
  • POJ 2728(最优比率生成树+01规划)

                                                                                                    Desert King
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 25729   Accepted: 7143

    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<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<cstring>
    #include<map>
    #include<stack>
    #include<set>
    #include<vector>
    #include<algorithm>
    #include<string.h>
    typedef long long ll;
    typedef unsigned long long LL;
    using namespace std;
    const int INF=0x3f3f3f3f;
    const double eps=0.0000000001;
    const int N=100000+10;
    const int MAX=1000+10;
    int vis[N];
    double x[N],y[N],z[N];
    double w[MAX][MAX],v[MAX][MAX];
    double low[N];
    int n;
    double fun(double a,double b,double c,double d){
        double ans=(a-c)*(a-c)+(b-d)*(b-d);
        return sqrt(ans);
    }
    int prime(double x){
        double sum=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)low[i]=v[0][i]-x*w[0][i];
        vis[0]=1;
        for(int i=0;i<n;i++){
            int k;
            double maxx=INF*1.0;
            for(int j=0;j<n;j++)if(vis[j]==0&&low[j]<maxx){
                maxx=low[j];
                k=j;
            }
            if(maxx==1.0*INF)break;
            sum=sum+maxx;
            vis[k]=1;
            for(int j=0;j<n;j++)
            if(vis[j]==0&&low[j]>v[k][j]-x*w[k][j])
            low[j]=v[k][j]-x*w[k][j];
        }
        if(sum>0)return 1;
        else
            return 0;
    }
    int main(){
        while(scanf("%d",&n)!=EOF){
            if(n==0)break;
            double maxx=0;
            double minn=INF*1.0;
            for(int i=0;i<n;i++)scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
            for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++){
                double t=fun(x[i],y[i],x[j],y[j]);
                w[i][j]=w[j][i]=t;
                v[i][j]=v[j][i]=fabs(z[i]-z[j]);
                maxx=max(v[i][j],maxx);
                minn=min(minn,t);
            }
            double low=0.0;
            double high=maxx/minn;
            double ans;
            while(low+eps<high){
                double mid=(low+high)/2;
                if(prime(mid)){
                    ans=mid;
                    low=mid;
                }
                else
                    high=mid;
            }
            printf("%.3f
    ",ans);
        }
    }
  • 相关阅读:
    透明代理和匿名代理的区别
    WinForm webbrowser控件的使用
    c# WebBrowser开发参考资料--杂七杂八
    C# Winform WebBrowser控件
    使用webBrowser进行C#和JS通讯
    webBrowser 应用编程函数总结
    C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
    利用WebBrowser控件实现百度自动搜索
    c#winform使用WebBrowser 大全
    ros在QT下编程
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6910892.html
Copyright © 2011-2022 走看看