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.
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://blog.csdn.net/guozizheng001/article/details/51044710
我看这个博客学的讲的特别好
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #include <ctype.h> 6 #include <set> 7 #include <map> 8 #include <queue> 9 #include <stack> 10 #include <iostream> 11 using namespace std; 12 #define bug printf("****** "); 13 const int maxn = 1e6 + 10; 14 #define rtl rt<<1 15 #define rtr rt<<1|1 16 const double eps=1e-6; 17 int n,vis[1010]; 18 double mp[1010][1010],p[1010]; 19 struct node { 20 double x,y,z; 21 }a[1010]; 22 double cost(double mid,int i,int j){ 23 return mid*mp[i][j]-1.0*(abs(a[i].z-a[j].z)); 24 } 25 int pri(double mid) { 26 for (int i=0 ;i<=n ;i++) vis[i]=0,p[i]=-100000000; 27 p[0]=0; 28 double ret=0; 29 for (int i=0 ;i<n ;i++ ) { 30 int idx=-1; 31 double maxx=-110000000.0; 32 for (int j=0 ;j<n ;j++){ 33 if (vis[j]) continue; 34 if (p[j]>maxx) { 35 maxx=p[j]; 36 idx=j; 37 } 38 } 39 if (idx==-1) break; 40 vis[idx]=1; 41 ret+=maxx; 42 for (int j=0 ;j<n ;j++) { 43 if (vis[j]) continue; 44 p[j]=max(p[j],cost(mid,idx,j)); 45 } 46 } 47 if (ret>eps) return 1; 48 return 0; 49 } 50 int main() { 51 while(scanf("%d",&n),n){ 52 for (int i=0 ;i<n ;i++) 53 scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z); 54 for (int i=0 ;i<n ;i++) 55 for (int j=i ;j<n ;j++) 56 mp[i][j]=mp[j][i]=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y)); 57 double high=100,low=0,mid; 58 int m=50; 59 while(m--){ 60 mid=(low+high)/2; 61 if (pri(mid)) high=mid; 62 else low=mid; 63 } 64 printf("%.3f ",low); 65 } 66 return 0; 67 }