poj2728 Desert King【最优比率生成树】

传送门:http://poj.org/problem?id=2728

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

题解传送门:https://www.cnblogs.com/Fatedayt/archive/2012/03/05/2380888.html
代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
const double inf = 1e10;
const int maxn = 1005;
const double eps = 1e-6;
struct node{
    int to;
    double len,cost;
    node(){}
    node(int to,double len,double cost):to(to),len(len),cost(cost){}
};
int n,tot;
double p[maxn][3];
double dis[maxn];
bool vis[maxn];
vector<node>e[maxn];
inline double distance(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)*1.0+(y1-y2)*(y1-y2)*1.0);
}
double prim(double L)
{
    for(int i=0;i<maxn;++i){
        dis[i]=inf;
        vis[i]=false;
    }
    vis[1]=true;
    dis[1]=0.0;
    for(int i=0;i<e[1].size();i++){
        int v=e[1][i].to;
        dis[v]=e[1][i].cost-L*e[1][i].len;
    }
    double sum=0;
    for(int i=1;i<n;i++){
        int u;
        double minn=inf;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&minn-dis[j]>eps){
                minn=dis[j];
                u=j;
            }
        }
        vis[u]=true;
        sum+=minn;
        for(int j=0;j<e[u].size();j++){
            int v=e[u][j].to;
            if(!vis[v]&&dis[v]>e[u][j].cost-L*e[u][j].len){
                dis[v]=e[u][j].cost-L*e[u][j].len;
            }
        }
    }
    return sum;
}
double work()
{
    double l=0,r=100.0,mid,temp;
    while(r-l>eps){
        mid=(l+r)/2.0;
        temp=prim(mid);
        if(temp>=-eps) l=mid;
        else r=mid;
    }
    return l;
}
int main()
{
    while(scanf("%d",&n),n){
        tot=0;
        for(int i=0;i<maxn;++i){
            e[i].clear();
        }
        for(int i=1;i<=n;i++){
            scanf("%lf%lf%lf",&p[i][0],&p[i][1],&p[i][2]);
        }
        for(int i=1;i<n;i++){
            for(int j=i+1;j<=n;j++){
                double len=distance(p[i][0],p[i][1],p[j][0],p[j][1]);
                double cost=fabs(p[j][2]-p[i][2]);
                e[i].push_back(node(j,len,cost));
                e[j].push_back(node(i,len,cost));
            }
        }
        double ans=work();
        printf("%.3f\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/blue_kid/article/details/79919107
今日推荐