Desert King
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 21923 |
|
Accepted: 6123 |
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 is connected to he capital village would be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant the.
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 would be is only one T O 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, villages and be built horizontally. Since Every, villages is at different altitudes, they concluded, each channel between, villages needed a vertic Al Water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the villages. The cost of the channel is the height of the lifter. You should notice the village is at a different altitude, and different channels can ' t share a lifter. Channels can intersect safely and no three villages is on the same line.
As King David's prime scientist and programmer, you're asked to find out the best solution to build the channels.
Input
There is several test cases. Each test case starts with a line containing a number N (2 <= N <=), 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 are the altitude. The first village is the capital. A test Case with N = 0 ends the input, and should not being processed.
Output
For each test case, output one line containing a decimal number, which are the minimum ratio of overall cost of the channel s to the total length. This number should is rounded three digits after the decimal point.
Sample Input
40 0 00 1 11 1 21 0 30
Sample Output
1.000
Source
Beijing 2005 topic: give you n points, each with x, y, z coordinates, build a three-dimensional spanning tree, is the height of the sum and horizontal distance of the sum ratio of the smallest, to find this ratio of AC code
#include <stdio.h> #include <string.h> #include <math.h> #define INF 1<<30struct s{double x, Y, Z;} Ponit[1010];int n,vis[1010];d ouble len[1010][1010],high[1010][1010],map[1010][1010];d ouble hlen (struct s a,struct s B ) {return fabs (A.Z-B.Z);} Double dis (struct s a,struct s b) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} Double Prim (double mid) {int i,j;for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {map[i][j]=high[i][j]-mid*len[i][j];}} memset (vis,0,sizeof (Vis)); Vis[1]=1;double sum=0;for (i=2;i<=n;i++) {int flag=-1;double minn=inf;for (j=1;j<=n; J + +) {if (!vis[j]&&map[1][j]<minn) {flag=j;minn=map[1][j];}} if (flag==-1) break;vis[flag]=1;sum+=map[1][flag];for (j=1;j<=n;j++) {if (!vis[j]&&map[1][j]>map[flag ][j]) {map[1][j]=map[flag][j];}}} return sum;} int main () {//int n;while (scanf ("%d", &n)!=eof,n) {int i,j;for (i=1;i<=n;i++) scanf ("%lf%lf%lf", &ponit[i].x , &ponit[i].y,&ponit[i].z); for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {Len[i][j]=len[j][i]=dIs (Ponit[i],ponit[j]); High[i][j]=high[j][i]=hlen (Ponit[i],ponit[j]);}} Double A,b,mid;a=0;b=100;while (b>=a) {mid= (a+b)/2;double Temp=prim (mid), if (Fabs (temp) <=0.00005) break;if ( temp>=0.00005) A=mid;elseb=mid;} printf ("%.3lf\n", mid);}}
POJ topic 2728 Desert King (optimal ratio spanning tree)