Desert King (poj 2728 optimal rate Spanning Tree 0-1 score planning), poj2728
Language:DefaultDesert King
Time Limit:3000 MS |
|
Memory Limit:65536 K |
Total Submissions:22113 |
|
Accepted:6187 |
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 |
Connecting n villages tells the 3D coordinates of each village. The distance between villages is the horizontal distance, and the cost is the vertical height difference, calculate the ratio of the minimum cost to the length of the village.
Train of Thought: Classic 01 score planning problems, refer to the great god explain should be able to understand: http://www.cnblogs.com/Fatedayt/archive/2012/03/05/2380888.html
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <stack> # include <vector> # include <set> # include <queue> # pragma comment (linker, "/STACK: 102400000,102400000") # define pi acos (-1.0) # define eps 1e-6 # define lson rt <1, l, mid # define rson rt <1 | 1, mid + 1, r # define FRE (I, a, B) for (I = a; I <= B; I ++) # define FREE (I, a, B) for (I = a; I> = B; I --) # define FRL (I, a, B) for (I = a; I <B; I ++) # define FRLL (I,, b) for (I = a; I> B; I --) # define mem (t, v) memset (t), v, sizeof (t )) # define sf (n) scanf ("% d", & n) # define sff (a, B) scanf ("% d", & a, & B) # define sfff (a, B, c) scanf ("% d", & a, & B, & c) # define pf printf # define DBG pf ("Hi \ n") typedef long ll; using namespace std; # define INF 0x3f3f3f # define mod 1000000009 const int maxn = 1005; Const int MAXN = 2005; const int MAXM = 200010; const int N = 1005; double x [maxn], y [maxn], z [maxn]; double dist [maxn], mp [maxn] [maxn], len [maxn] [maxn], cost [maxn] [maxn]; bool vis [maxn]; int pre [maxn]; int n; double Dis (int I, int j) {return sqrt (x [I]-x [j]) * (x [I]-x [j]) + (y [I]-y [j]) * (y [I]-y [j]);} double prim (double r) {int I, j, now; double mi, c = 0, l = 0; for (I = 0; I <n; I ++) {dist [I] = INF; for (j = 0; j <n; j ++) {mp [I] [j] = Cost [I] [j]-r * len [I] [j] ;}} for (I = 0; I <n; I ++) {dist [I] = mp [I] [0]; pre [I] = 0; vis [I] = false;} dist [0] = 0; vis [0] = true; for (I = 1; I <n; I ++) {mi = INF; now =-1; for (j = 0; j <n; j ++) {if (! Vis [j] & mi> dist [j]) {mi = dist [j]; now = j ;}} if (now =-1) break; vis [now] = true; c ++ = cost [pre [now] [now]; l + = len [pre [now] [now]; for (j = 0; j <n; j ++) {if (! Vis [j] & dist [j]> mp [now] [j]) {dist [j] = mp [now] [j]; pre [j] = now ;}}return c/l;} int main () {# ifndef ONLINE_JUDGE freopen ("C:/Users/lyf/Desktop/IN.txt ", "r", stdin); # endif int I, j; while (sf (n) {if (n = 0) break; for (I = 0; I <n; I ++) scanf ("% lf", & x [I], & y [I], & z [I]); for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {len [I] [j] = Dis (I, j); cost [I] [j] = fabs (z [I]-z [j]);} double r = 0, rate; // The initial value of r iteration is 0 while (1) {rate = r; r = prim (r); if (fabs (r-rate) <eps) break;} printf ("%. 3f \ n ", r);} return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.