POJ728 Desert King <二分+MST>

Problem

Desert King

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 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 villages, 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 villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. 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 should notice that each village is 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 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(2N1000)N (2 \le N \le 1000), which is the number of villages. Each of the following NN lines contains three integers, xx, yy and zz (0x,y<104,0z<107)(0\le x, y < 10^4, 0 \le z < 10^7). (x,y)(x, y) is the position of the village and zz is the altitude. The first village is the capital. A test case with N=0N = 0 ends the input, and should 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 should be rounded three digits after the decimal point.

Sample Input

1
2
3
4
5
6
4
0 0 0
0 1 1
1 1 2
1 0 3
0

Sample Output

1
1.000

Translation

给出nn个村庄和n2n^2条路,以及每条路的长度和代价,要求选出一棵生成树使得其路径总代价除以路径总长度最小。

标签:最优比例生成树 二分答案 MST

Solution

本题是最优比例生成树的裸题。
熟悉二分答案的套路。

注意到如果一个答案xx可作为平均值,那么比x大的所有答案均可作为平均值,问题具有二分性
于是二分答案,即最后的平均值,对于每个答案,构造最小生成树判断可行性,具体如下:
对于第ii条边,长度为lil_i,代价为cic_i,那么将此边的权值设为wi=cilitansw_i=c_i-l_i*tans,跑一遍最小生成树,如果总权值小于00,则有可行解。
注意此题为稠密图,应用PrimePrime

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define MAX_N 1000
using namespace std;
int n;
double map[MAX_N+5][MAX_N+5], val[MAX_N+5][MAX_N+5];
struct Point {double x, y, h;} p[MAX_N+5];
double calc(double x1, double y1, double x2, double y2) {return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
bool check(double t) {
double ret = 0;
double dis[MAX_N+5];
bool vis[MAX_N+5];
memset(dis, 127, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[1] = 0;
for (int i = 0; i < n; i++) {
int tv = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && (!tv || dis[j] < dis[tv]))
tv = j;
vis[tv] = 1, ret += dis[tv];
for (int j = 1; j <= n; j++)
if (!vis[j])
dis[j] = min(dis[j], val[tv][j]-map[tv][j]*t);
}
return ret < 0;
}
double bi_search() {
double l = 0, r = 1e5, mid;
while (abs(l-r) > 1e-6) {
mid = (l+r)/2;
if (check(mid)) r = mid;
else l = mid;
}
return mid;
}
int main() {
while (scanf("%d", &n) && n) {
for (int i = 1; i <= n; i++) scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].h);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
map[i][j] = map[j][i] = calc(p[i].x, p[i].y, p[j].x, p[j].y),
val[i][j] = val[j][i] = abs(p[i].h-p[j].h);
printf("%.3f\n", bi_search());
}
return 0;
}