01 score planning + prim POJ2728 Desert King, primpoj2728

Source: Internet
Author: User

01 score planning + prim POJ2728 Desert King, primpoj2728
Desert King

Time Limit:3000 MS   Memory Limit:65536 K
Total Submissions:26009   Accepted:7219

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. ages 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 available ages, 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 versions ages and be built horizontally. since every two versions ages are at different altitudes, they concluded that each channel between two versions ages needed a vertical water lifter, which lift water up or let water flow down. the length of the channel is the horizontal distance between the two ages. the cost of the channel is the height of the lifter. you shoshould notice that each village is at a different altitude, and different channels can't share a lifter. channels can intersect safely and no three ages 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 ages. 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 shoshould 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 shoshould be 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 question: there are n villages in different coordinates and heights. Now we need to supply water to all the villages, as long as there is a path between the two villages, the distance between the construction pipe is the Euclidean distance between the coordinates, and the cost is the altitude difference. Now, the scheme is required to minimize the ratio of the cost to the distance, that is, to find an optimal rate spanning tree. Prim is very efficient in finding a dense graph ......
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 using namespace std; 7 const double acc = 1e-7; 8 const double inf = 1e15; 9 int n; 10 struct data {11 double x, y, z; 12} node [1010]; 13 double l, r, mid; 14 double dis [1010] [1010], h [1010] [1010], w [1010]; 15 bool check [1010]; 16 double ds (double x1, double y1, double x2, double y2) {17 return sqrt (x1-x2) * (x1-x2) + (y1 -Y2) * (y1-y2); 18} 19 double prim (double c) {// coefficient-coefficient 20 double ret = 0.0; 21 for (int I = 1; I <= n; I ++) w [I] = inf; // double assigns the initial value 22 memset (check, 0, sizeof (check )); 23 w [1] = 0.0; 24 for (int I = 1; I <= n; I ++) {25 double mn = inf; 26 int k; 27 for (int j = 1; j <= n; j ++) 28 if (! Check [j] & w [j] <mn) mn = w [j], k = j; 29 check [k] = 1; 30 ret + = mn; 31 for (int j = 1; j <= n; j ++) 32 if (! Check [j] & w [j]> h [k] [j]-c * dis [k] [j]) 33 w [j] = h [k] [j]-c * dis [k] [j]; 34} 35 return ret; 36} 37 int main () {38 while (scanf ("% d", & n) {39 if (! N) return 0; 40 for (int I = 1; I <= n; I ++) {41 scanf ("% lf ", & node [I]. x, & node [I]. y, & node [I]. z); 42 for (int j = 1; j <= I; j ++) {43 dis [I] [j] = dis [j] [I] = ds (node [I]. x, node [I]. y, node [j]. x, node [j]. y); 44 h [I] [j] = h [j] [I] = abs (node [I]. z-node [j]. z); 45} 46} 47 l = 0.0; 48 r = 10000.0; 49 while (r-l> acc) {50 mid = (l + r) * 1.0/2; 51 if (prim (mid)> 0) l = mid; 52 else r = mid; 53} 54 printf ("%. 3f \ n ", mid); 55} 56}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.