[网络流24题]P3357 最长k可重线段集

费用流好题,k重区间plus版本
https://www.luogu.com.cn/problem/P3357

从 \(k\) 重区间变成 \(k\) 重线段,仍旧只看 \(x\) 轴的重叠。
这样就多出来一个问题,对于平行于 \(x\) 轴的线段,还向上一题那样连边会变成自环,于是T了两个点。考虑拆点,每个点拆成 \(2n\) 和 \(2n+1\). 剩下的正常连就行了~

点击查看代码
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
typedef long long ll;
#define endl '\n'
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0)
#define P pair<int, int>
#define endl '\n'
using namespace std;

// #define int long long

const int maxn = 500 * 2 + 10;
const int inf = 0x3f3f3f3f;
int n1, n2, cnt_edge = 1, S, T;
int head[maxn];
int dis[maxn];
bool vis[maxn];
struct edge {
    int to, nxt;
    int flow, cost;
} e[(maxn * maxn) << 2];
inline void add(int u, int v, int w, int c) {
    e[++cnt_edge].nxt = head[u];
    head[u] = cnt_edge;
    e[cnt_edge].to = v;
    e[cnt_edge].flow = w;
    e[cnt_edge].cost = c;
}
inline void addflow(int u, int v, int w, int c) {
    add(u, v, w, c);
    add(v, u, 0, -c);
}
inline bool spfa(int on) {
    memset(vis, 0, sizeof(vis));
    if (on == 1)
        for (int i = 0; i <= T; i++) dis[i] = inf;
    else
        for (int i = 0; i <= T; i++) dis[i] = -inf;
 
    queue<int> q;
    q.push(S);
    dis[S] = 0;
    vis[S] = 1;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        vis[x] = 0;
        for (int i = head[x]; i; i = e[i].nxt) {
            int y = e[i].to;
            // cout << "->" << y << endl;
            if ((on == 1 && e[i].flow && dis[y] > dis[x] + e[i].cost) ||
                (on == -1 && e[i].flow && dis[y] < dis[x] + e[i].cost)) {
                dis[y] = dis[x] + e[i].cost;
                if (!vis[y]) q.push(y), vis[y] = 1;
            }
        }
    }
    if (on == 1)
        return dis[T] != inf;
    else
        return dis[T] != -inf;
}
ll dfs(int x, ll lim) {
    vis[x] = 1;
    if (x == T || lim <= 0) return lim;
    int res = lim;
    for (int i = head[x]; i; i = e[i].nxt) {
        int y = e[i].to;
        if (dis[y] != dis[x] + e[i].cost || e[i].flow <= 0 || vis[y]) continue;
        ll tmp = dfs(y, min(res, e[i].flow));
        res -= tmp;
        e[i].flow -= tmp;
        e[i ^ 1].flow += tmp;
        if (res <= 0) break;
    }
    return lim - res;
}
inline ll Dinic(int on) {
    ll res = 0, cost = 0;
    while (spfa(on)) {
        ll flow = dfs(S, inf);
        res += flow, cost += flow * dis[T];
    }
    return cost;
}

int nn, k;
int lx[maxn], rx[maxn], ly[maxn], ry[maxn], a[maxn << 1], len[maxn];
int cdis(int i) {
    return int(sqrt(1ll * (lx[i] - rx[i]) * (lx[i] - rx[i]) +
                    1ll * (ly[i] - ry[i]) * (ly[i] - ry[i])));
}
signed main() {
    // IOS;
    cin >> nn >> k;
    for (int i = 1; i <= nn; i++) {
        cin >> lx[i] >> ly[i] >> rx[i] >> ry[i];
        if (lx[i] > rx[i])
            swap(lx[i], rx[i]), swap(ly[i], ry[i]);
        len[i] = cdis(i);
        lx[i] <<= 1, rx[i] <<= 1;
        if (lx[i] == rx[i])
            rx[i]++;
        else lx[i]++;
        a[i] = lx[i], a[i + nn] = rx[i];
    }
    sort(a + 1, a + nn * 2 + 1);
    int n = unique(a + 1, 1 + a + nn * 2) - a - 1;
    S = n + 1, T = S + 1;
    for (int i = 1; i <= nn; i++) {
        int L = lower_bound(a + 1, a + 1 + n, lx[i]) - a;
        int R = lower_bound(a + 1, a + 1 + n, rx[i]) - a;
        addflow(L, R, 1, len[i]);
    }

    for (int i = 1; i < n; i++) {
        addflow(i, i + 1, inf, 0);
    }
    addflow(S, 1, k, 0);
    addflow(n, T, k, 0);
    // cout << "build c\n";
    cout << Dinic(-1) << endl;
    return 0;
}
上一篇:js(jQuery)获取时间的方法及常用时间类


下一篇:模板合集