[BZOJ 1937][Shoi2004]Mst 最小生成树

时间:2024-01-10 16:56:44

传送门

$ \color{red} {solution:} $

对于每条树边\(i\),其边权只可能变小,对于非树边\(j\),其边权只可能变大,所以对于任意非树边覆盖的树边有 \(wi - di <= wj + dj\),
变形一下 \(wi - wj <= di +dj\), 而这一部分正是带权二分图匹配,可以使用\(KM\)算法
不会\(KM\)算法的同学这里有篇算法介绍;

 #include <bits/stdc++.h>
using namespace std;

const int maxn = 10010, inf = 0x3f3f3f3f;

template <typename T> inline bool check_Max(T &x, const T &y) {
    return x < y ? x = y, false : true;
}

template <typename T> inline bool check_Min(T &x, const T &y) {
    return x > y ? x = y, false : true;
}

struct edge {int u, v, w; } E[maxn];

int v[maxn], to[maxn], head[maxn], pos[maxn], p;
inline void link(int a, int b, int c) {
    v[++ p] = b; to[p] = head[a]; head[a] = p; pos[p] = c;
}

inline void init() {
    p = 1; memset(head, 0, sizeof(head));
}

int dep[maxn], fa[maxn], rfe[maxn];
inline void dfs(int u, int f) {
    dep[u] = dep[f] + 1; fa[u] = f;
    for ( int i = head[u]; i; i = to[i]) if( v[i] ^ f)
        rfe[v[i]] = i >> 1, dfs(v[i], u);
}

int a[1010][1010], pre[maxn], slack[maxn], vis[maxn], mat[maxn], tot;
int lv[maxn], rv[maxn];

inline void aug(int s) {
    for ( int i = 0; i <= tot; ++ i) slack[i] = inf, vis[i] = pre[i] = 0;
    int u = 0; mat[u] = s;
    do {
        int now = mat[u], d = inf, nxt; vis[u] = 1;
        for ( int v = 1; v <= tot; ++ v) if( !vis[v]){
            if( !check_Min(slack[v], lv[now] + rv[v] - a[now][v]))
                pre[v] = u;
            if( !check_Min(d, slack[v])) nxt = v;
        }
        for ( int i = 0; i <= tot; ++ i)
            if( vis[i]) lv[mat[i]] -= d, rv[i] += d;
            else slack[i] -= d;
        u = nxt;
    } while ( mat[u]);
    while ( u) mat[u] = mat[pre[u]], u = pre[u];
}

int n, m;

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    init();
    scanf("%d%d", &n, &m); tot = max(n-1, m-n+1);
    for ( int i = 1; i <= m; ++ i) {
        scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w);
        if( E[i].u > E[i].v) swap(E[i].u, E[i].v);
    }
    for ( int i = 1, u, v; i < n; ++ i) {
        scanf("%d%d", &u, &v); if( u > v) swap(u, v);
        for ( int k = 1; k <= m; ++ k) if( E[k].u == u && E[k].v == v) {
            vis[k] = 1, link(u, v, E[k].w), link(v, u, E[k].w); break;
        }
    }
    dfs(1, 0);
    int num = 0;
    for ( int i = 1; i <= m; ++ i) if( !vis[i]) {
        int u = E[i].u, v = E[i].v, c; ++ num;
        while ( u ^ v) {
            if( dep[u] < dep[v]) swap(u, v);
            c = rfe[u];
            a[c][num] = pos[c << 1] - E[i].w; u = fa[u];
        }
    }
    for ( int i = 1; i <= tot; ++ i)
        for ( int k = 1; k <= tot; ++ k) check_Max(lv[i], a[i][k]);
    for ( int i = 1; i < n; ++ i) aug(i);
    int ans = 0;
    for ( int i = 1; i <= tot; ++ i) ans += lv[i] + rv[i];
    printf("%d\n", ans);
    return 0;
}

[BZOJ 1937][Shoi2004]Mst 最小生成树