POJ - 2728 Desert King

Posted 小小超plus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ - 2728 Desert King相关的知识,希望对你有一定的参考价值。

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
二分加生成树
技术分享图片
 1 #include <iostream>
 2 using namespace std;
 3 #include<string.h>
 4 #include<set>
 5 #include<stdio.h>
 6 #include<math.h>
 7 #include<queue>
 8 #include<map>
 9 #include<algorithm>
10 #include<cstdio>
11 #include<cmath>
12 #include<cstring>
13 #include <cstdio>
14 #include <cstdlib>
15 #include<stack>
16 #include<vector>
17 struct lll
18 {
19     double  x,y,z;
20 }TM[1100];
21 const double MAX=1e9*1.0;
22 double a[1100][1100];
23 double b[1100][1100];
24 double c[1100][1100];
25 int mp[1100];
26 int n;
27 double d[1100];
28 int P(double mid)
29 {
30     for(int i=1;i<=n;i++)
31     {
32         for(int j=1;j<=n;j++)
33         {
34             c[i][j]=b[i][j]-mid*a[i][j];
35         }
36     }
37     for(int i=1;i<=n;i++)
38         d[i]=MAX;
39     double sum=0;
40     int t;
41     int u=1;
42     memset(mp,0,sizeof(mp));
43     mp[1]=1;
44     for(int i=1;i<=n;i++)
45     {
46         t=0;
47         double s=MAX;
48         for(int j=1;j<=n;j++)
49         {
50             d[j]=min(d[j],c[u][j]);
51             if(d[j]<s&&!mp[j])
52             {
53                 s=d[j];
54                 t=j;
55             }
56         }
57         if(t==0)
58             break;
59         sum+=s;
60         //cout<<sum<<endl;
61         u=t;
62         mp[t]=1;
63     }
64     //cout<<mid<<"_"<<sum<<endl;
65     return sum>=0;
66 }
67 int  main()
68 {
69     while(cin>>n,n)
70     {
71             for(int i=1;i<=n;i++)
72         scanf("%lf %lf %lf",&TM[i].x,&TM[i].y,&TM[i].z);
73     double  star=0.0,over=10000000.0;
74     for(int i=1;i<=n;i++)
75     {
76         for(int j=1;j<=n;j++)
77         {
78             a[i][j]=sqrt(pow(TM[i].x-TM[j].x,2)+pow(TM[i].y-TM[j].y,2));
79             a[j][i]=a[i][j];
80             b[i][j]=fabs(TM[i].z-TM[j].z);
81             b[j][i]=b[i][j];
82             over=max(over,b[i][j]/a[i][j]);
83         }
84     }
85     double mid;
86     while(over-star>0.00000001)
87     {
88         //cout<<star<<"_"<<over<<endl;
89         mid=(star+over)/2;
90         if(P(mid))
91             star=mid;
92         else
93             over=mid;
94 
95     }
96     printf("%.3lf\n",star);
97     }
98     return 0;
99 }
View Code

 








以上是关于POJ - 2728 Desert King的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2728 Desert King

POJ - 2728 Desert King

POJ——T 2728 Desert King

POJ2728 Desert King

POJ2728Desert King - 01分数规划

POJ 2728 Desert King (01分数规划)