POJ2728-Desert King(最优比率生成树)

题目链接

http://poj.org/problem?id=2728

题意

给定一张稠密图,每条边有花费w和长度l,要求图的一棵最优比率生成树,定义如下:对这棵树,sum(w) / sum(l)最小

思路

0-1规划问题
令条件x: sum(w) / sum(l) 不超过x,只需要二分一下x即可
那么问题就转化为了: sum(w) / sum(l) ≤ x
另g(x) = sum(w) - x * sum(l)。则只需要满足g(x)≤0即可
然后问题就转化成了求新图:每条边的权值为w - x * l的最小生成树,并且要最小生成树的权值和≤0
由于图是稠密图,因此用prim算法

代码

#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;

#define PI acos(-1.0)
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define mp make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "wb", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)
#define pr(x) cout << #x << " = " << x << endl
#define lc o << 1
#define rc o << 1 | 1
#define pl() cout << endl
#define CLR(a, x) memset(a, x, sizeof(a))
#define FILL(a, n, x) for (int i = 0; i < n; i++) a[i] = x

const int maxn = 1000 + 5;
const int maxm = 1000000 + 5;
const double INF = 0x7f7f7f7f;
const double eps = 1e-6;

int n, m;
double l[maxn][maxn], w[maxn][maxn], g[maxn][maxn];
struct node {
    int x, y, z;
} a[maxn];

double mincost[maxn];
bool use[maxn];
bool prim() {
    FILL(mincost, n, INF);
    CLR(use, 0);
    mincost[0] = 0;
    double res = 0;
    while (1) {
        int v = -1;
        for (int u = 0; u < n; u++) {
            if (!use[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
        }
        if (v == -1) break;
        use[v] = 1;
        res += mincost[v];
        for (int u = 0; u < n; u++) mincost[u] = min(mincost[u], g[v][u]);
    }
    return res <= 0;
}

bool judge(double x) {
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) 
            g[i][j] = g[j][i] = w[i][j] - x * l[i][j];
    }
    return prim();
}

double dist(int i, int j) {
    return sqrt(sqr(double(a[i].x - a[j].x)) + sqr(double(a[i].y - a[j].y)));
}

int main() {
    while (scan(n) && n) {
        for (int i = 0; i < n; i++) scan3(a[i].x, a[i].y, a[i].z);
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                l[i][j] = l[j][i] = dist(i, j);
                w[i][j] = w[j][i] = fabs(double(a[i].z - a[j].z));
            }
        }
        double L = 0.0, R = INF, M;
        while (R - L > eps) {
            M = (L + R) / 2;
            if (judge(M)) R = M;
            else L = M;
        }
        printf("%.3lf\n", M);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值