网络流--最大流--hlpp(预流推进)模板
生活随笔
收集整理的這篇文章主要介紹了
网络流--最大流--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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络流--最大流--Dinic模板矩阵版
- 下一篇: ACM卡常处理办法(虽然我到现在没遇到)