Desert King(POJ-2728)(二分解法)

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

题意:多组数据,每组给出 n 个点的三维坐标,每两点之间存在一条边,花费为两点之间的高度差,利润为两点之间水平直线距离,现在要在这 n 个点中选若干条边构成一个生成树,要求最小化选的边的花费和与利润和的比率

思路:01 分数规划中的最优比率生成树模型,本质是要最小化 \frac{\sum value[i]}{\sum cost[i]},其中 value 为花费,即两点间的高度差,cost 为利润,即两点间的水平距离,使用 Prim 算法求最小生成树,然后对边重赋值,再利用二分求最优比率即可

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;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值