http://poj.org/problem?id=2728
Desert King
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 18595 | Accepted: 5245 |
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
Source
【题解】:
题意:给定三维的点,求这样一棵树,使得高度差的和与水平距离的和的比值最小
这题是很显然的最优比例生成树,不能用贪心求出cost/len,再建MST。
注意输出用% .3f 用%.3lf会WA
【code】:
1 /** 2 Judge Status:Accepted Memory:732K 3 Time:454MS Language:G++ 4 Code Length:1772B Author:cj 5 */ 6 7 #include <iostream> 8 #include <stdio.h> 9 #include <algorithm> 10 #include <math.h> 11 #include <string.h> 12 13 #define N 1010 14 #define INF 1000000000 15 //using namespace std; //加了这句居然报CE,什么情况没搞懂 16 17 double dis[N]; 18 int pre[N],vis[N]; 19 int n; 20 21 struct Nod 22 { 23 int x,y,z; 24 }node[N]; 25 26 double distance(Nod a,Nod b) 27 { 28 return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))); 29 } 30 31 int abs(int x){return x>0?x:-x;} 32 33 double prim(double r) 34 { 35 memset(vis,0,sizeof(vis)); 36 int i; 37 for(i=2;i<=n;i++) 38 { 39 dis[i] = abs(node[1].z-node[i].z) - distance(node[1],node[i])*r; 40 pre[i]=1; 41 } 42 dis[1] = 0; 43 vis[1] = 1; 44 double cost=0,len=0; 45 int j; 46 for(i=1;i<n;i++) 47 { 48 double mins = INF; 49 int k = -1; 50 for(j=2;j<=n;j++) 51 { 52 if(!vis[j]&&mins>dis[j]) 53 { 54 mins = dis[j]; 55 k = j; 56 } 57 } 58 if(k==-1) break; 59 vis[k] = 1; 60 cost += abs(node[pre[k]].z-node[k].z); 61 len += distance(node[pre[k]],node[k]); 62 for(j=2;j<=n;j++) 63 { 64 double val = abs(node[k].z-node[j].z) - distance(node[k],node[j])*r; 65 if(!vis[j]&&dis[j]>val) 66 { 67 dis[j] = val; 68 pre[j] = k; 69 } 70 } 71 } 72 return cost/len; 73 } 74 75 int main() 76 { 77 while(~scanf("%d",&n)&&n) 78 { 79 int i; 80 for(i=1;i<=n;i++) 81 { 82 scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].z); 83 } 84 double a=0,b=0; //初始r为0 85 while(1) 86 { 87 b = prim(a); 88 if(fabs(a-b)<1e-4) break; 89 a=b; 90 } 91 printf("%.3f ",b); //printf("%.3lf ",b); %.3lf居然WA 改 %.3f就AC 神马神马情况 92 } 93 return 0; 94 }