【BZOJ2654】tree(MST,凸优化)

3 篇文章 0 订阅
2 篇文章 0 订阅

Description

给你一个无向带权连通图,每条边是黑色或白色。让你求一棵最小权的恰好有need条白色边的生成树。


Solution

二分一个值,将白色边减去该值,检查最后MST中白边的数量即可。


Code

/************************************************
 * Au: Hany01
 * Date: Aug 13th, 2018
 * Prob: BZOJ2654 Tree
 * Email: hany01@foxmail.com
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 5e4 + 5, maxm = 1e5 + 5;

int n, m, tot1, tot0, fa[maxn];

struct Edge { int u, v, w, c; } E1[maxm], E0[maxm], E[maxm];
inline bool operator < (Edge A, Edge B) { return A.w < B.w; }

int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }

inline PII calc(int dt) {
    static int cur1, cur0, cnt, fu, fv, num, sum;
    cnt = num = sum = 0, cur1 = cur0 = 1;
    For(i, 1, n) fa[i] = i;
    For(i, 1, m)
        if (E0[cur0].w - dt <= E1[cur1].w) E[i] = E0[cur0 ++], E[i].w -= dt, E[i].c = 1;
        else E[i] = E1[cur1 ++], E[i].c = 0;
    For(i, 1, m) {
        fu = find(E[i].u), fv = find(E[i].v);
        if (fu != fv) {
            fa[fu] = fv, cnt += E[i].c, sum += E[i].w;
            if ((++ num) + 1 == n) return mp(sum, cnt);
        }
    }
}

int main()
{
#ifdef hany01
    freopen("bzoj2654.in", "r", stdin);
    freopen("bzoj2654.out", "w", stdout);
#endif

    static int uu, vv, ww, l, r, mid, p;

    n = read(), m = read(), p = read();
    For(i, 1, m) {
        uu = read() + 1, vv = read() + 1, ww = read();
        if (read()) E1[++ tot1] = Edge{uu, vv, ww};
        else E0[++ tot0] = Edge{uu, vv, ww};
    }
    sort(E0 + 1, E0 + 1 + tot0), sort(E1 + 1, E1 + 1 + tot1);
    E0[tot0 + 1].w = E1[tot1 + 1].w = INF1;

    for (l = -200, r = 200; l < r; ) {
        mid = (l + r) >> 1;
        if (calc(mid).y >= p) r = mid;
        else l = mid + 1;
    }
    printf("%d\n", calc(l).x + l * p);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值