# 14325: 【原4325】Desert King

## 题目

### 题目描述

author: chen 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4325

# Desert King

Time Limit: 30S Memory Limit: 65536K

## 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 (2 <= N <= 1000), 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 is the altitude. The first village is the capital. A test case with N = 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

```
4
0 0 0
0 1 1
1 1 2
1 0 3
0
```

## Sample Output

```
1.000
```

## Source

Beijing 2005

## zqy2018's solution

```
#include <bits/stdc++.h>
#define INF 2000000000
#define MOD 1000000007
#define MAXN 200005
#define REP(temp, init_val, end_val) for (int temp = init_val; temp <= end_val; ++temp)
#define REPR(temp, init_val, end_val) for (int temp = init_val; temp >= end_val; --temp)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int read(){
int f = 1, x = 0;
char c = getchar();
while (c < '0' || c > '9'){if(c == '-') f = -f; c = getchar();}
while (c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
return f * x;
}
int n;
int x[1005], y[1005], z[1005];
double dis[1005][1005];
double dd[1005];
bool vis[1005];
inline double sqr(int t){
return t * t;
}
double getdis(int u, int v){
return sqrt(sqr(x[u] - x[v]) + sqr(y[u] - y[v]));
}
void init(){
REP(i, 1, n){
x[i] = read(), y[i] = read(), z[i] = read();
}
REP(i, 1, n)
REP(j, i + 1, n)
dis[j][i] = dis[i][j] = getdis(i, j);
}
bool ok(double t){
REP(i, 1, n)
dd[i] = 1e13;
dd[1] = 0;
memset(vis, 0, sizeof(vis));
double mst = 0;
REP(i, 1, n){
double mini = 1e13;
int minp = 0;
REP(j, 1, n)
if (!vis[j] && mini > dd[j])
mini = dd[j], minp = j;
vis[minp] = true;
mst += dd[minp];
REP(j, 1, n)
if (!vis[j])
dd[j] = min(dd[j], abs(z[minp] - z[j]) - t * dis[minp][j]);
}
return mst <= 1e-8;
}
void solve(){
double l = 0, r = 1e5;
while (r - l > 1e-4){
double mid = (l + r) * 0.5;
if (ok(mid))
r = mid;
else l = mid;
}
printf("%.3lf\n", l);
}
int main(){
while (n = read()){
init();
solve();
}
return 0;
}
```