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.
Main topic
The plane gives $n$ points, 22 have a connecting edge, a side has two weights: distance and height difference, ask a spanning tree make $\frac{\sum dist_i}{\sum height_i}$ maximum
Ideas
As with "POJ2976", it is possible to change the judgment to prim.
/*************************************************author:lrj124*created Time:2018.10.01.20:38*mail : [email protected]*problem:poj2728************************************************/#include <cstdio> #include <cmath>using namespace Std;const int maxn = + 10;double A[MAXN][MAXN],B[MAXN][MAXN],TMP[MAXN][MAXN], min[maxn];struct Node {int x, y, Z,} p[maxn];bool vis[maxn];int n,e[maxn];inline bool Prim (double x) {for (int i = 1;i < = n;i++) for (int j = 1;j <= n;j++) tmp[i][j] = a[i][j]-x*b[i][j];for (int i = 0;i <= n;i++) {Vis[i] = false; Min[i] = 1000000000;} MIN[1] = 0;e[1] = 0;double ans = 0;for (int i = 1;i <= n;i++) {int minnum = 0;for (int j = 1;j <= n;j++) if (Min[minn UM] > Min[j] &&!vis[j]) Minnum = J;vis[minnum] = True;ans + = tmp[e[minnum]][minnum];for (int j = 1;j <= n;j + +) if (Tmp[minnum][j] < Min[j] &&!vis[j]) {min[j] = tmp[minnum][j];e[j] = minnum;}} return ans <= 0;} int main () {//freopEn ("poj2728.in", "R", stdin),//freopen ("Poj2728.out", "w", stdout), while (scanf ("%d", &n), N) {for (int i = 1;i <= n; i++) scanf ("%d%d%d", &p[i].x,&p[i].y,&p[i].z); for (int i = 1;i <= n;i++) for (int j = 1;j <= n;j++) {a[i][ J] = Fabs (P[I].Z-P[J].Z); B[i][j] = sqrt ((p[i].x-p[j].x) * (p[i].x-p[j].x) + (P[I].Y-P[J].Y) * (P[I].Y-P[J].Y));} Double L = 0,r = 1000000000;while (r-l >= 1e-6) {Double mid = (l+r)/2;if (Prim (mid)) R = Mid;else L = mid;} printf ("%.3f\n", L);} return 0;}
POJ2728 Desert King-01 score Plan