[網路流24題]P3358 最長k可重區間
阿新 • • 發佈:2021-10-21
費用流好題!學到許多。
https://www.luogu.com.cn/problem/P3358
題意
這道題題意一開始不知道為啥給我腦補成選幾個區間求其並長度的最大值。。其實是給定 \(n\) 個區間,需要選取一個區間集合,使得區間覆蓋的所有點被覆蓋不超過 \(k\) 次,使得選取區間的總長度最大(不是並長度)。
Tutorial
可以發現如果兩個區間之間不相交,那麼他們倆就可以隨便選。但是如果出現相交,那麼他們之間就存在 \(k\) 的限制。如果將一個區間拆成兩個點,容量為1,費用為區間長度,表示一個區間最多貢獻一次長度。這樣一來就會發現整個圖變成了幾組組內不會相交,但是組間會相交的區間組。就像電流的串聯並聯一樣,接下來只要把這些區間串起來,向源點連一條容量為 \(k\)
注意離散化。
點選檢視程式碼
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <vector> #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; typedef long long ll; const int maxn = 500 * 2 + 10; const ll inf = 1e18; int n1, n2, cnt_edge = 1, S, T; int head[maxn]; ll dis[maxn]; bool vis[maxn]; struct edge { int to, nxt; ll flow, cost; } e[(maxn * maxn) << 2]; inline void add(int u, int v, ll w, ll 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, ll w, ll 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; ll 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 id(int x, int y, int in) { return 2 * ((n1 * 2 + x - 2) * (x - 1) / 2 + y - 1) + in; } int nn, k; int l[maxn], r[maxn], a[maxn]; int main() { cin >> nn >> k; for (int i = 1; i <= nn; i++) { cin >> l[i] >> r[i]; if (l[i] > r[i]) swap(l[i], r[i]); a[i] = l[i], a[i + nn] = r[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, l[i]) - a; int R = lower_bound(a + 1, a + 1 + n, r[i]) - a; addflow(L, R, 1, r[i] - l[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); return 0; }