A spanning tree is obtained to minimize the cost and distance ratio.
01 score planning, consider rewriting the equation.
∑costi,j∑disi,j≥p \frac{\sum cost_{i,j}}{\sum Dis_{i,j}}\geq p
where P is the final answer, deformed
∑ (costi,j−pxdisi,j) ≥0 \sum (Cost_{i,j}-p\times dis_{i,j}) \geq 0
Consider the two-point answer, and then construct a new diagram according to the above, because to make all the possible formulas on the left must be satisfied, so the minimum spanning tree, if satisfied with >0, then P is small, <0, then P is big.
#include <cstdio> #include <cmath> #define for (i,j,k) for (int i=j;i<=k;++i) #define SQR (x) ((x) * (x)) const
int N = 1005;
struct Edge {double cost, dis; Double weight (double mid) {return cost-mid * DIS;}}
E[n][n];
int n;
Double Prim (int s, double mid) {static int near[n];
static double cost[n];
Double ans = 0;
for (i,1,n) near[i] = s, cost[i] = E[s][i].weight (mid);
Near[s] =-1;
for (i,1,n-1) {double mi = 1e18;
int v =-1;
for (J,1,n) if (Near[j]!=-1 && cost[j] < mi) v = j, mi = cost[j];
if (v!=-1) {Near[v] =-1; ans + = cost[v]; for (J,1,n) if (Near[j]!=-1 && e[v][j].weight (mid) < COST[J]) near[j] = V, cost[j] = E[V][J].W
Eight (mid);
} return ans;
int main () {static int x[n], y[n], z[n]; while (scanf ("%d", &n)!= EOF && N) {for (i,1,n) scanf ("%d%d%d", &x[i), &y[i], &AMp;z[i]);
for (I,1,n) for (j,1,n) {e[i][j].cost = Fabs (Z[i]-z[j]);
E[i][j].dis = sqrt (SQR (x[i)-x[j]) + SQR (y[i)-y[j]);
Double L = 0, r = m, mid;
while (R-l > 1e-6) {mid = (R + L)/2;
if (Prim (1, mid) >= 0) L = mid;
else R = Mid;
printf ("%.3f\n", L);
return 0; }
Desert King
Time limit:3000ms Memory limit:65536k
Total submissions:22996 accepted:6449 Description
David The great has just become the king of a desert country. To win the respect's his people, he decided to build channels all over his country to bring water to every. Villages which are connected to he capital village would be watered. As the dominate ruler and the symbol of wisdom in the country, his needs to build the channels in a most elegant.
After the days of the study, he finally figured him. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio's overall cost of the channels to the total length must is minimized. He just needs to build the necessary channels to bring water-all villages, which means there'll be is only one way t O Connect each village to the capital.
His engineers surveyed the country and recorded the position and altitude to each village. All of the channels must go straight between two villages and is built horizontally. Since every two villages are at different altitudes, they concluded this each channel between two villages needed a vertic Al Water lifter, which can lift water up or let water. 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 are should notice that each village be 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 are asked to find 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), and 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 The village is the capital. A test Case with N = 0 ends the input, and should is 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
4
0 0 0
0 1 1
1 1 2
1 0 3
0 Sample Output
1.000