日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网络流--最大流--hlpp(预流推进)模板

發(fā)布時間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网络流--最大流--hlpp(预流推进)模板 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//500ms 秒掉洛谷推流問題 #include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <queue> using namespace std; typedef long long LL; typedef long long F_type; const int MAXN = 1.2e3 + 10, INF = 0x3f3f3f3f; const LL LINF = (LL)INF << 32 | INF; struct Edge {int v, rev;F_type cap;Edge(int a, F_type b, int c) : v(a), rev(c), cap(b) {} }; const F_type maxf=LINF; F_type exflow[MAXN]; int h[MAXN], cnt[MAXN]; int ht, N, S, T, labelcnt; vector<Edge> G[MAXN]; vector<int> hq[MAXN]; void clear(int n = MAXN - 1) {ht = labelcnt = 0;for (int i = 0; i <= n; i++)G[i].clear(); } void addEdge(int u, int v, F_type cap) {G[u].emplace_back(v, cap, G[v].size());G[v].emplace_back(u, 0, G[u].size() - 1); } void update(int u, int newh) {++labelcnt;if (h[u] != N + 1)--cnt[h[u]];h[u] = newh;if (newh == N + 1)return;++cnt[ht = newh];if (exflow[u] > 0)hq[newh].push_back(u); } void globalRelabel() {queue<int> q;for (int i = 0; i <= N + 1; i++)hq[i].clear();for (int i = 0; i <= N; i++)h[i] = N + 1, cnt[i] = 0;q.push(T);labelcnt = ht = h[T] = 0;while (!q.empty()){int u = q.front();q.pop();for (Edge& e : G[u]){if (h[e.v] == N + 1 && G[e.v][e.rev].cap){update(e.v, h[u] + 1);q.push(e.v);}}ht = h[u];} } void push(int u, Edge& e) {if (exflow[e.v] == 0)hq[h[e.v]].push_back(e.v);F_type df = min(exflow[u], e.cap);e.cap -= df;G[e.v][e.rev].cap += df;exflow[u] -= df;exflow[e.v] += df; } void discharge(int u) {int nxth = N + 1;for (Edge& e : G[u])if (e.cap){if (h[u] == h[e.v] + 1){push(u, e);if (exflow[u] <= 0)return;}elsenxth = min(nxth, h[e.v] + 1);}if (cnt[h[u]] > 1)update(u, nxth);elsefor (; ht >= h[u]; hq[ht--].clear()){for (int& j : hq[ht])update(j, N + 1);} } F_type maxFlow(int s, int t, int n) {S = s, T = t, N = n;memset(exflow, 0, sizeof(exflow));exflow[S] = maxf;exflow[T] = -maxf;globalRelabel();for (Edge& e : G[S])push(S, e);for (; ht >= 0; --ht){while (!hq[ht].empty()){int u = hq[ht].back();hq[ht].pop_back();discharge(u);if (labelcnt > (N << 2))globalRelabel();}}return exflow[T] + maxf; }int main() {int n, m, s, t, u, v, w;scanf("%d%d%d%d", &n, &m, &s, &t);while (m--){scanf("%d%d%d", &u, &v, &w);addEdge(u, v, w);}printf("%d", maxFlow(s, t, n));return 0; }

?

總結

以上是生活随笔為你收集整理的网络流--最大流--hlpp(预流推进)模板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。