• DOJ1187 : 重建家园 (分数规划 && 二分 && kruskal)


    最优答案一定是一颗树

    那么二分比值,不断kruskal找到最大可以满足的解就可以了

    代码如下

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    typedef double db;
    
    struct Node{
        int a, b, c, d;
        db w;
        inline void init() {
            scanf("%d %d %d %d", &a, &b, &c, &d);
        }
        bool operator < (const Node &o) const{
            return w < o.w;
        }
    }a[10010];
    int n, m, f, fa[410];
    
    int find(int x) {
        return (fa[x] == x) ? x : fa[x] = find(fa[x]);
    }
    
    inline bool kruskal(db x) {
        for (int i = 1; i <= n; i++)    fa[i] = i;
        for (int i = 1; i <= m; i++)
            a[i].w = (db)a[i].c + (db)a[i].d * x;
        sort(a + 1, a + m + 1);
        db ans = 0;
        int tot = 0;
        for (int i = 1; i <= m; i++) {
            int x = find(a[i].a);
            int y = find(a[i].b);
            if (x != y) {
                fa[x] = y;
                ans += a[i].w;
                tot++;
            }
            if (ans > f)    return false;
            if (tot == n - 1)    return true;
        }
    }
    
    int main() {
        scanf("%d %d %d", &n, &m, &f);
        for (int i = 1; i <= m; i++)
            a[i].init();
        db l = 0, r = (db)f / (n - 1);
        while (r - l > 0.000001) {
            db mid = (l + r) / 2;
            if (kruskal(mid))    l = mid;
            else r = mid;
        }
        printf("%.4lf", l);
        return 0;
    }
  • 相关阅读:
    AC3 encoder flow
    AC3 IMDCT
    AC3 Rematrix
    AC3 channel coupling
    AC3 mantissa quantization and decoding
    AC3 bit allocation
    AC3 exponent coding
    C# 判断字符串为数字 int float double
    vs 修改默认的调试浏览器
    visio 如何扩大画布大小
  • 原文地址:https://www.cnblogs.com/cminus/p/7467486.html
Copyright © 2020-2023  润新知