# poj2728:22Aの最优比例生成树！做的这么辛苦怎么可能有福利！

2016年3月17日 1.13k 次阅读 0 人点赞

Desert King
 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 23144 Accepted: 6486

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

MD我懒得解释了，上代码！

Source Code

 Problem: 2728 User: aclolicon Memory: 24540K Time: 1610MS Language: G++ Result: Accepted
• Source Code
```#include<cstdio>
#include<iostream>
#include<cmath>
#define MAXN 1010
#define INF 0x7ffffffff
#define EPS 1e-5//精度控制
using namespace std;
struct position{
int x, y;
}pos[MAXN];//记录输入村庄的值
double g[MAXN][MAXN], mc[MAXN];//记录prim存图以及最短距离，以及是否已经走过
bool used[MAXN];//记录prim是否已经走过
double dist[MAXN][MAXN], cost[MAXN], dis[MAXN];//记录两村之间欧几里得距离， 高度
double h[MAXN][MAXN];
int pre[MAXN];
int n;
int ans;//记录答案
//Based on the formula: sigma((costi - costj) - ans * d) == 0
double calcdist(position a, position b){
return sqrt(((double)a.x - (double)b.x)*((double)a.x - (double)b.x) + ((double)a.y - (double)b.y)*((double)a.y - (double)b.y));
}
void erase(){//Prim初始化
for (int i = 0; i < n; i++){
mc[i] = INF;
used[i] = 0;
pre[i] = 0;
}
mc = 0;
}
double prim(double bin){//prim算法算出最小生成树
erase();
double res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++){
g[i][j] = h[i][j] - bin * dist[i][j];
//			cout << i << ',' << j << ':' << g[i][j] << endl;
}
while(1) {
int v = -1;
for (int i = 0; i < n; i++) if (!used[i] && (v == -1||mc[v] > mc[i])) v = i;
if (v == -1) break;
used[v] = 1;
res += mc[v];
//		cout << v << ',' << res << endl;
for (int i = 0; i < n; i++) mc[i] = min(mc[i], g[v][i]);
}
return res;
//	cout << tcost << ',' << tdist << endl;
//	return sum;
}

void solve(){
double a = 0, l = 0, r = 100000.0;
while (r - l > EPS)  {
double mid = (l + r) / 2.0;
a = prim(mid);
if (a >= 0) l = mid;
else r = mid;
}
printf("%.3fn", r);
}
void init(){
for (int i = 0; i < n; i++)
cin >> pos[i].x >> pos[i].y >> cost[i];
for (int i = 0; i < n; i++)//在初始化做好预处理，到时候直接调用不用重复计算
for (int j = 0; j < i; j++){
dist[j][i] = dist[i][j] = calcdist(pos[i],pos[j]);
h[i][j] = h[j][i] = abs(cost[i] - cost[j]);
//		                cout << i << ',' << j << '!' << h[i][j] << ':' << dist[i][j] << endl;
}

}
int main(){
while(cin >> n){
if (n == 0) break;
init();
solve();
}
return 0;
}```