返回顶部
大江东去,浪淘尽,千古风流人物。故垒西边,人道是,三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。江山如画,一时多少豪杰。遥想公瑾当年,小乔初嫁了,雄姿英发。羽扇纶巾,谈笑间,樯橹灰飞烟灭。故国神游,多情应笑我,早生华发。人生如梦,一尊还酹江月。

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\n",l);
    }
    return 0;
}
View Code

 

posted @ 2019-08-27 17:16  plysc  阅读(128)  评论(0编辑  收藏  举报