POJ 2728:Desert King

来源:互联网 发布:邮政网络培训学院官网 编辑:程序博客网 时间:2024/05/16 07:46


Desert King
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 25635 Accepted: 7117

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

40 0 00 1 11 1 21 0 30

Sample Output

1.000

题目意思:

给出N个村庄的坐标他们之间修建渠道的距离是水平方向的距离,而修建这个渠道的花费是竖直方向的距离。

求一个生成树,是铺设一米的管道费用最小。

最优比率生成树。


#include<iostream>#include<stdio.h>#include<queue>#include<string.h>#include<algorithm>#include<math.h>#define inf 0x3f3f3f3fusing namespace std;const int maxn = 1005;const double eps = 0.0001;int N;struct point{    double x;    double y;    double h;}p[maxn];double dis(struct point p1,struct point p2){    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}double cost(struct point p1,struct point p2){    return fabs(p1.h-p2.h);}double prim(double r){    double lowcost[maxn];    int vis[maxn];    int closest[maxn];    double Min;    double length = 0;    double Cost = 0;    int u;    for(int i = 1; i <= N; i++)    {        lowcost[i] = cost(p[1],p[i])-r*dis(p[1],p[i]);        closest[i] = 1;        vis[i] = 0;    }    lowcost[1] = 0;    vis[1] = 1;    for(int i = 1; i < N; i++)    {        Min = inf;        for(int j = 2; j <= N; j++)        {            if(vis[j]==0 && lowcost[j]<Min)            {                Min = lowcost[j];                u = j;            }        }        vis[u] = 1;        int pre = closest[u];        length += dis(p[u],p[pre]);        Cost += cost(p[u],p[pre]);        for(int j = 2; j <= N; j++)        {            double temp = cost(p[j],p[u])-r*dis(p[u],p[j]);            if(vis[j]==0 && temp < lowcost[j])            {                lowcost[j] = temp;                closest[j] = u;            }        }    }    return Cost/length;   ///米/元}int main(){    while(~scanf("%d",&N))    {        if(N == 0) break;        for(int i = 1; i <= N; i++)        {            scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].h);        }        double ans,temp;        ans = 0;        while(true)        {            temp = prim(ans);            if(fabs(temp-ans)<eps) break;                ans = temp;        }        printf("%.3lf\n",ans);    }    return 0;}


0 0