Desert King (POJ-2728) (two decomposition)

Problem 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

That Italy: a plurality of sets of data, each given three-dimensional coordinates of the point n, between 2.1 per side, it takes a height difference between two points, the profit for the horizontal straight line distance between two points, now in the n selected points to form a plurality of spanning edges, requires a minimum of expense and selected from the edge and the profit ratio of

Ideas: the optimal ratio spanning tree model 01 scores planning, in essence, is to minimize  \frac{\sum value[i]}{\sum cost[i]}, where value is spent, that is, the height difference between two points, cost is the profit that the level of distance between two points using Prim for the Minimum the spanning tree, and then reassigned to the edge, then use dichotomy can seek the optimal ratio of

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;
 
struct Node {
    int x, y, z;
} node[N];
int n;
double cost[N][N], value[N][N]; //花费、利润
bool vis[N];
double dis[N];
 
double getCost(int x1, int y1, int x2, int y2) {
    double dis = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
    return sqrt(dis);
}
double getValue(int x, int y) { 
    return abs(x - y); 
}
double Prim(double x) { // Prim求MST
    dis[1] = 0;
    for (int i = 2; i <= n; i++) //对边重赋值
        dis[i] = value[1][i] - cost[1][i] * x;
 
    memset(vis, false, sizeof(vis));
    vis[1] = true;
 
    int k;
    double mst = 0.0;
    for (int i = 2; i <= n; i++) {
        double minCost = INF;
        for (int j = 2; j <= n; j++) {
            if (!vis[j] && dis[j] < minCost) {
                minCost = dis[j];
                k = j;
            }
        }
        vis[k] = true;
 
        for (int j = 1; j <= n; j++)
            if (!vis[j] && dis[j] > value[k][j] - cost[k][j] * x)
                dis[j] = value[k][j] - cost[k][j] * x;
    }
    for (int i = 1; i <= n; i++)
        mst += dis[i];
    return mst;
}
int main() {
    while (scanf("%d", &n) != EOF && n) {
        for (int i = 1; i <= n; i++) {
            scanf("%d%d%d", &node[i].x, &node[i].y, &node[i].z);
            for (int j = 1; j <= i - 1; j++) {
                double Cost = getCost(node[i].x, node[i].y, node[j].x, node[j].y); //计算花费
                double Value = getValue(node[i].z, node[j].z); //计算价值
                cost[i][j] = cost[j][i] = Cost;
                value[i][j] = value[j][i] = Value;
            }
        }
 
        double left = 0, right = 100000.0;
        while (right - left > EPS) { //对mst的权值进行二分
            double mid = (left + right) / 2.0;
            if (Prim(mid) >= 0)
                left = mid;
            else
                right = mid;
        }
        printf("%.3f\n", right);
    }
    return 0;
}

 

Released 1871 original articles · won praise 702 · Views 1.94 million +

Guess you like

Origin blog.csdn.net/u011815404/article/details/102669706