【LOJ6014】【网络流 24 题】最长 k 可重区间集

Description

https://loj.ac/problem/6014


Solution

考虑费用流:
先离散化,
S 1 连边,流量为 k ,费用为 0
从最后一个点向 T 连边,流量为 k ,费用为 0
i i + 1 连边,流量为 + ,费用为 0
对于每一条线段,从左端连向右端,流量为 1 ,费用为线段长度。
跑MCMF即可。


Code

/************************************************
 * Au: Hany01
 * Date: Jul 12th, 2018
 * Prob: LOJ6014
 * Email: [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#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 ALL(a) (a).begin(), (a).end()
#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 = 1005, maxm = maxn << 2;

int n, e = 1, k, beg[maxn], v[maxm], nex[maxm], f[maxm], w[maxm], dis[maxn], vis[maxn], S, T, ls[maxn], tot, l[maxn], r[maxn], cost;

inline void add(int uu, int vv, int ff, int ww, int fl = 1) {
    if (fl) ww = -ww;
    v[++ e] = vv, f[e] = ff, w[e] = ww, nex[e] = beg[uu], beg[uu] = e;
    if (fl) add(vv, uu, 0, -ww, 0);
}

inline bool BFS()
{
    static queue<int> q;
    For(i, 1, T) dis[i] = INF;
    Set(vis, 0), dis[S] = 0, q.push(S);
    while (!q.empty()) {
        int u = q.front(); q.pop(), vis[u] = 0;
        for (register int i = beg[u]; i; i = nex[i])
            if (f[i] && chkmin(dis[v[i]], dis[u] + w[i]))
                if (!vis[v[i]]) vis[v[i]] = 1, q.push(v[i]);
    }
    return dis[T] != INF;
}

int DFS(int u, int flow)
{
    if (u == T) return flow;
    int t, res = flow;
    vis[u] = 1;
    for (register int i = beg[u]; i; i = nex[i])
        if (f[i] && dis[v[i]] == dis[u] + w[i] && !vis[v[i]]) {
            t = DFS(v[i], min(flow, f[i])), f[i] -= t, f[i ^ 1] += t, cost += w[i] * t;
            if (!(res -= t)) return flow;
        }
    return flow - res;
}

int main()
{
#ifdef hany01
    File("loj6014");
#endif

    n = read(), k = read();
    For(i, 1, n) {
        ls[++ tot] = l[i] = read(), ls[++ tot] = r[i] = read();
        if (l[i] > r[i]) swap(l[i], r[i]);
    }
    sort(ls + 1, ls + 1 + tot), tot = unique(ls + 1, ls + 1 + tot) - ls - 1;
    T = (S = tot + 1) + 1, add(S, 1, k, 0), add(tot, T, k, 0);
    For(i, 1, tot - 1) add(i, i + 1, INF, 0);
    For(i, 1, n) add(lower_bound(ls + 1, ls + 1 + tot, l[i]) - ls, lower_bound(ls + 1, ls + 1 + tot, r[i]) - ls, 1, r[i] - l[i]);

    while (BFS()) do Set(vis, 0), DFS(S, INF); while (vis[T]);
    printf("%d\n", -cost);

    return 0;
}
//恨人间、会少离多,万古千秋今夕。
//    -- 张埜《夺锦标·七夕》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/81022978