Section 6.1
parent
8ebfeb77a5
commit
fe9d24592e
@ -0,0 +1,76 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <cctype>
|
||||||
|
#include <cstring>
|
||||||
|
inline int min(int a, int b) { return a < b ? a : b; }
|
||||||
|
inline void read(int &x)
|
||||||
|
{
|
||||||
|
int ch = x = 0;
|
||||||
|
while (!isdigit(ch = getchar()));
|
||||||
|
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
|
||||||
|
}
|
||||||
|
const int N = 205, inf = 0x3f3f3f3f;
|
||||||
|
int adj[N], nxt[N << 1], to[N << 1], cap[N << 1], cur[N], cnt[N], dis[N], fa[N], ecnt;
|
||||||
|
inline void addEdge_impl_(int f, int t, int c)
|
||||||
|
{
|
||||||
|
nxt[ecnt] = adj[f];
|
||||||
|
adj[f] = ecnt;
|
||||||
|
to[ecnt] = t;
|
||||||
|
cap[ecnt] = c;
|
||||||
|
ecnt++;
|
||||||
|
}
|
||||||
|
inline void addEdge(int f, int t, int c)
|
||||||
|
{
|
||||||
|
addEdge_impl_(f, t, c);
|
||||||
|
addEdge_impl_(t, f, 0);
|
||||||
|
}
|
||||||
|
int ISAP(int S, int T)
|
||||||
|
{
|
||||||
|
int flow = 0;
|
||||||
|
for (int i = 0; i < N; i++) dis[i] = N - 1;
|
||||||
|
int len = 0, x;
|
||||||
|
static int que[N];
|
||||||
|
dis[que[len++] = T] = 0;
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
for (int e = adj[x = que[i]]; ~e; e = nxt[e])
|
||||||
|
if (cap[e ^ 1] && dis[to[e]] > dis[x] + 1)
|
||||||
|
dis[que[len++] = to[e]] = dis[x] + 1;
|
||||||
|
memset(cnt, 0, sizeof(cnt));
|
||||||
|
for (int i = 0; i < N; i++) cur[i] = adj[i], cnt[dis[i]]++;
|
||||||
|
x = S;
|
||||||
|
while (dis[S] < N - 1)
|
||||||
|
{
|
||||||
|
if (x == T)
|
||||||
|
{
|
||||||
|
int curFlow = inf;
|
||||||
|
for (x = T; x != S; x = to[fa[x] ^ 1]) curFlow = min(curFlow, cap[fa[x]]);
|
||||||
|
for (x = T; x != S; x = to[fa[x] ^ 1]) cap[fa[x]] -= curFlow, cap[fa[x] ^ 1] += curFlow;
|
||||||
|
flow += curFlow, x = S;
|
||||||
|
}
|
||||||
|
bool needRetreat = true;
|
||||||
|
for (int e = cur[x]; needRetreat && ~e; e = nxt[e])
|
||||||
|
if (cap[e] && dis[x] == dis[to[e]] + 1)
|
||||||
|
needRetreat = false, cur[x] = e, fa[x = to[e]] = e;
|
||||||
|
if (needRetreat)
|
||||||
|
{
|
||||||
|
int nd = N - 2;
|
||||||
|
for (int e = adj[x]; ~e; e = nxt[e])
|
||||||
|
if (cap[e]) nd = min(nd, dis[to[e]]);
|
||||||
|
if (--cnt[dis[x]] == 0) break;
|
||||||
|
++cnt[dis[x] = nd + 1];
|
||||||
|
cur[x] = adj[x];
|
||||||
|
if (x != S) x = to[fa[x] ^ 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flow;
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
while (~scanf("%d%d", &m, &n))
|
||||||
|
{
|
||||||
|
memset(adj, -1, sizeof(adj)), ecnt = 0;
|
||||||
|
for (int i = 0, x, y, z; i < m; i++)
|
||||||
|
scanf("%d%d%d", &x, &y, &z), addEdge(x, y, z);
|
||||||
|
printf("%d\n", ISAP(1, n));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
inline int min(int a, int b) { return a < b ? a : b; }
|
||||||
|
const int inf = 0x3f3f3f3f;
|
||||||
|
int adj[205], nxt[405], to[405], cap[405], cur[205], dis[405], ecnt;
|
||||||
|
inline void addEdge_impl_(int f, int t, int c)
|
||||||
|
{
|
||||||
|
nxt[ecnt] = adj[f];
|
||||||
|
adj[f] = ecnt;
|
||||||
|
to[ecnt] = t;
|
||||||
|
cap[ecnt] = c;
|
||||||
|
ecnt++;
|
||||||
|
}
|
||||||
|
inline void addEdge(int f, int t, int c)
|
||||||
|
{
|
||||||
|
addEdge_impl_(f, t, c);
|
||||||
|
addEdge_impl_(t, f, 0);
|
||||||
|
}
|
||||||
|
int n, m;
|
||||||
|
bool bfs()
|
||||||
|
{
|
||||||
|
static int que[205];
|
||||||
|
for (int i = 1; i <= n; i++)
|
||||||
|
cur[i] = adj[i], dis[i] = inf;
|
||||||
|
int len = dis[1] = 0;
|
||||||
|
que[len++] = 1;
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
for (int e = adj[que[i]]; ~e; e = nxt[e])
|
||||||
|
if (cap[e] && dis[to[e]] > dis[que[i]] + 1)
|
||||||
|
dis[que[len++] = to[e]] = dis[que[i]] + 1;
|
||||||
|
return dis[n] < inf;
|
||||||
|
}
|
||||||
|
int dinic(const int u, const int flow)
|
||||||
|
{
|
||||||
|
if (u == n)
|
||||||
|
return flow;
|
||||||
|
int res = 0;
|
||||||
|
for (int e = cur[u], curFlow; ~e; e = nxt[e])
|
||||||
|
if (cap[e] && dis[u] == dis[to[e]] - 1)
|
||||||
|
if (curFlow = dinic(to[e], min(cap[e], flow - res)))
|
||||||
|
res += curFlow, cap[e] -= curFlow, cap[e ^ 1] += curFlow, cur[u] = e;
|
||||||
|
if (res != flow)
|
||||||
|
dis[u] = inf;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
while (~scanf("%d%d", &m, &n))
|
||||||
|
{
|
||||||
|
ecnt = 0;
|
||||||
|
memset(adj, -1, sizeof(adj));
|
||||||
|
for (int i = 0, x, y, z; i < m; i++)
|
||||||
|
scanf("%d%d%d", &x, &y, &z), addEdge(x, y, z);
|
||||||
|
int ans = 0;
|
||||||
|
while (bfs())
|
||||||
|
ans += dinic(1, inf);
|
||||||
|
printf("%d\n", ans);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Binary file not shown.
Loading…
Reference in new issue