POJ 2728 Desert King 01分数规划 最优比率生成树

来源:互联网 发布:sql 2005什么用 编辑:程序博客网 时间:2024/04/30 23:35

求一个生成树使得费用与距离比值最小。

01分数规划的话,考虑改写式子。

costi,jdisi,jp

其中p是最终答案,变形得
(costi,jp×disi,j)0

考虑二分答案,然后按照上式构造一个新图,因为要让所有左边可能的式子都必须满足,所以求最小生成树,如果满足>0,那么p小了,<0,那么p大了。

#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].weight(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], &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 = 100, 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 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

0 0