「ZJOI2011」最小割
Posted zsbzsb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了「ZJOI2011」最小割相关的知识,希望对你有一定的参考价值。
「ZJOI2011」最小割
传送门
建出最小割树,然后暴力计算任意两点之间最小割即可。
多组数据记得初始化。
参考代码:
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 160, __ = 3010 * 2 + 10, INF = 2147483647;
int tot = 1, head[_], Cap[__ << 1], Q[_];
struct Edge { int ver, cap, nxt; } edge[__ << 1];
inline void Add_edge(int u, int v, int d) { edge[++tot] = (Edge) { v, d, head[u] }, head[u] = tot ; }
inline void link(int u, int v, int d) { Add_edge(u, v, d), Cap[tot] = d, Add_edge(v, u, 0), Cap[tot] = 0; }
int n, m, dep[_], cur[_], p[_], t1[_], t2[_];
int num; struct node { int x, y, z; } G[_];
int fa[10][_], mn[10][_], ans[_][_];
inline int bfs(int s, int t) {
int hd = 0, tl = 0;
memset(dep, 0, sizeof dep);
Q[++tl] = s, dep[s] = 1;
while (hd < tl) {
int u = Q[++hd];
for (rg int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].ver;
if (dep[v] == 0 && edge[i].cap > 0)
dep[v] = dep[u] + 1, Q[++tl] = v;
}
}
return dep[t] > 0;
}
inline int dfs(int u, int flow, int t) {
if (u == t) return flow;
for (rg int& i = cur[u]; i; i = edge[i].nxt) {
int v = edge[i].ver;
if (dep[v] == dep[u] + 1 && edge[i].cap > 0) {
int res = dfs(v, min(flow, edge[i].cap), t);
if (res) { edge[i].cap -= res, edge[i ^ 1].cap += res; return res; }
}
}
return 0;
}
inline int Dinic(int s, int t) {
for (rg int i = 2; i <= tot; ++i) edge[i].cap = Cap[i];
int res = 0;
while (bfs(s, t)) {
for (rg int i = 1; i <= n; ++i) cur[i] = head[i];
while (int d = dfs(s, INF, t)) res += d;
}
return res;
}
inline void solve(int l, int r) {
if (l == r) return ;
int s = p[l], t = p[l + 1];
G[++num] = (node) { s, t, Dinic(s, t) };
int p1 = 0, p2 = 0;
for (rg int i = l; i <= r; ++i) if (dep[p[i]]) t1[++p1] = p[i]; else t2[++p2] = p[i];
for (rg int i = 1; i <= p1; ++i) p[l + i - 1] = t1[i];
for (rg int i = 1; i <= p2; ++i) p[l + p1 + i - 1] = t2[i];
solve(l, l + p1 - 1), solve(l + p1, r);
}
inline void dfs(int u, int f) {
dep[u] = dep[f] + 1;
for (rg int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].ver; if (v == f) continue ;
fa[0][v] = u, mn[0][v] = edge[i].cap, dfs(v, u);
}
}
inline int calc(int x, int y) {
int res = INF;
if (dep[x] < dep[y]) swap(x, y);
for (rg int i = 8; ~i; --i)
if (dep[fa[i][x]] >= dep[y]) res = min(res, mn[i][x]), x = fa[i][x];
if (x == y) return res;
for (rg int i = 8; ~i; --i)
if (fa[i][x] != fa[i][y]) res = min(res, min(mn[i][x], mn[i][y])), x = fa[i][x], y = fa[i][y];
return min(res, min(mn[0][x], mn[0][y]));
}
inline void Main() {
read(n), read(m);
tot = 1, memset(head, 0, sizeof head);
for (rg int u, v, d; m--; ) read(u), read(v), read(d), link(u, v, d), link(v, u, d);
for (rg int i = 1; i <= n; ++i) p[i] = i;
num = 0, solve(1, n);
tot = 0, memset(head, tot, sizeof head);
for (rg int i = 1; i <= num; ++i) Add_edge(G[i].x, G[i].y, G[i].z), Add_edge(G[i].y, G[i].x, G[i].z);
fa[0][1] = 0, dfs(1, 0);
for (rg int i = 1; i <= 8; ++i)
for (rg int u = 1; u <= n; ++u)
fa[i][u] = fa[i - 1][fa[i - 1][u]], mn[i][u] = min(mn[i - 1][u], mn[i - 1][fa[i - 1][u]]);
for (rg int i = 1; i <= n; ++i) for (rg int j = i + 1; j <= n; ++j) ans[i][j] = calc(i, j);
int q; read(q);
for (rg int x, cnt; q--; ) {
read(x), cnt = 0;
for (rg int i = 1; i <= n; ++i) for (rg int j = i + 1; j <= n; ++j) cnt += ans[i][j] <= x;
printf("%d
", cnt);
}
}
int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
int T; read(T);
while (T--) Main(), puts("");
return 0;
}
以上是关于「ZJOI2011」最小割的主要内容,如果未能解决你的问题,请参考以下文章
bzoj2229: [Zjoi2011]最小割(分治最小割+最小割树思想)