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
分析
这题本质上是求 nnn 个点, mmm 条边,每条边有 a,ba, ba,b两个值,求一棵生成树,使得 ∑a∑b\dfrac{\sum a}{\sum b}∑b∑a最小。
不难想到 0/10/10/1 分数规划。
二分 ttt,使得对于所有边,∑a∑b≥t\dfrac{\sum a}{\sum b}\geq t∑b∑a≥t 恒成立。
等价于对于任意边,∑a−t∗b≥0\sum a-t*b \geq 0∑a−t∗b≥0,于是我们可以用最小生成树。
需要注意的是,本题给出的是稠密图,于是要用朴素的 primprimprim
代码如下
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define N 1005
#define eps 1e-6
#define inf 2147483647
using namespace std;
double f[N][N], d[N], h[N];
int x[N], y[N], v[N], n;
double get(int i, int j){
return sqrt(1.0 * (x[i] - x[j]) * (x[i] - x[j]) + 1.0 * (y[i] - y[j]) * (y[i] - y[j]));
}
int ok(double t){
int i, j, k;
for(i = 0; i <= n; i++)
for(j = 0; j <= n; j++) f[i][j] = inf;
double sum = 0;
memset(v, 0, sizeof(v));
for(i = 1; i <= n; i++){
for(j = i + 1; j <= n; j++){
f[i][j] = f[j][i] = 1.0 * fabs(h[i] - h[j]) - t * get(i, j);
}
}
for(i = 0; i <= n; i++) d[i] = f[1][i];
v[1] = 1;
for(i = 1; i < n; i++){
for(k = 0, j = 1; j <= n; j++) if(!v[j] && d[j] < d[k]) k = j;
v[k] = 1;
sum += d[k];
for(j = 1; j <= n; j++) d[j] = min(d[j], f[k][j]);
}
//printf("%.5lf %.2lf\n", t, sum);
return sum + eps > 0;
}
int main(){
int i, j;
double l, r, m;
while(scanf("%d", &n)){
if(!n) break;
for(i = 1; i <= n; i++) scanf("%d%d%lf", &x[i], &y[i], &h[i]);
l = 0, r = 100;
while(l + eps < r){
m = (l + r) / 2;
if(ok(m)) l = m;
else r = m;
}
printf("%.3f\n", l);
}
return 0;
}

面对沙漠国家供水挑战,国王决定建立覆盖全国的渠道网络。作为科学家和程序员,你需要找到一种方法来构建渠道,使得渠道总成本与总长度的比例最小化。这个问题涉及到多个村庄的位置和海拔,需要构建一个生成树,使得特定比例最小。
353

被折叠的 条评论
为什么被折叠?



