Section 6.2

master
大蒟蒻 9 years ago
parent fe9d24592e
commit 78b43fa180

@ -0,0 +1,64 @@
#include <cstdio>
#include <cstring>
inline int min(int a, int b) { return a < b ? a : b; }
const int inf = 0x3f3f3f3f, N = 1005, M = 50005;
int adj[N], nxt[M], to[M], cap[M], cost[M], dis[N], ecnt, idx;
inline void addEdge_impl_(int f, int t, int c, int w)
{
nxt[ecnt] = adj[f];
adj[f] = ecnt;
to[ecnt] = t;
cap[ecnt] = c;
cost[ecnt] = w;
ecnt++;
}
inline void addEdge(int f, int t, int c, int w)
{
addEdge_impl_(f, t, c, w);
addEdge_impl_(t, f, 0, -w);
}
int T;
bool bfs(int S)
{
memset(dis, inf, sizeof(dis)), idx++;
static int que[N << 1];
static bool inq[N];
int len = 0;
dis[que[len++] = S] = 0;
for (int i = 0; i < len; inq[que[i++]] = false)
for (int e = adj[que[i]]; ~e; e = nxt[e])
if (cap[e] && dis[to[e]] > dis[que[i]] + cost[e])
{
dis[to[e]] = dis[que[i]] + cost[e];
if (!inq[to[e]]) que[len++] = to[e];
}
return dis[T] < inf;
}
int dfs(int x, int rest)
{
static int vis[N];
if (x == T) return rest;
vis[x] = idx;
int flow = 0;
for (int e = adj[x], curFlow; rest && ~e; e = nxt[e])
if (vis[to[e]] != idx && cap[e] && dis[to[e]] == dis[x] + cost[e])
if (bool(curFlow = dfs(to[e], min(cap[e], rest))))
rest -= curFlow, flow += curFlow, cap[e] -= curFlow,
cap[e ^ 1] += curFlow;
return flow;
}
int main()
{
memset(adj, -1, sizeof(adj));
int n, m;
scanf("%d%d", &n, &m);
T = n + 1;
for (int i = 0, x, y, z; i < m; i++)
scanf("%d%d%d", &x, &y, &z), addEdge(x, y, 1, z), addEdge(y, x, 1, z);
addEdge(0, 1, 2, 0);
addEdge(n, T, 2, 0);
int ans = 0;
while (bfs(0)) ans += dis[T] * dfs(0, inf);
printf("%d", ans);
return 0;
}

@ -30,18 +30,16 @@ bool bfs()
dis[que[len++] = to[e]] = dis[que[i]] + 1;
return dis[n] < inf;
}
int dinic(const int u, const int flow)
int dinic(const int u, int rest)
{
if (u == n)
return flow;
int res = 0;
for (int e = cur[u], curFlow; ~e; e = nxt[e])
if (u == n) return rest;
int flow = 0;
for (int e = cur[u], curFlow; rest && ~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;
if ((curFlow = dinic(to[e], min(cap[e], rest))))
rest -= curFlow, flow += curFlow, cap[e] -= curFlow, cap[e ^ 1] += curFlow, cur[u] = e;
if (rest) dis[u] = inf;
return flow;
}
int main()
{
@ -52,9 +50,8 @@ int main()
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);
while (bfs()) ans += dinic(1, inf);
printf("%d\n", ans);
}
return 0;
}
}

@ -0,0 +1,64 @@
#include <cstdio>
#include <cstring>
inline int min(int a, int b) { return a < b ? a : b; }
const int inf = 0x3f3f3f3f, N = 1005, M = 50005;
int adj[N], nxt[M], to[M], cap[M], cost[M], dis[N], ecnt, idx;
inline void addEdge_impl_(int f, int t, int c, int w)
{
nxt[ecnt] = adj[f];
adj[f] = ecnt;
to[ecnt] = t;
cap[ecnt] = c;
cost[ecnt] = w;
ecnt++;
}
inline void addEdge(int f, int t, int c, int w)
{
addEdge_impl_(f, t, c, w);
addEdge_impl_(t, f, 0, -w);
}
int T;
bool bfs(int S)
{
memset(dis, inf, sizeof(dis)), idx++;
static int que[N << 1];
static bool inq[N];
int len = 0;
dis[que[len++] = S] = 0;
for (int i = 0; i < len; inq[que[i++]] = false)
for (int e = adj[que[i]]; ~e; e = nxt[e])
if (cap[e] && dis[to[e]] > dis[que[i]] + cost[e])
{
dis[to[e]] = dis[que[i]] + cost[e];
if (!inq[to[e]]) que[len++] = to[e];
}
return dis[T] < inf;
}
int dfs(int x, int rest)
{
static int vis[N];
if (x == T) return rest;
vis[x] = idx;
int flow = 0;
for (int e = adj[x], curFlow; rest && ~e; e = nxt[e])
if (vis[to[e]] != idx && cap[e] && dis[to[e]] == dis[x] + cost[e])
if (bool(curFlow = dfs(to[e], min(cap[e], rest))))
rest -= curFlow, flow += curFlow, cap[e] -= curFlow,
cap[e ^ 1] += curFlow;
return flow;
}
int main()
{
memset(adj, -1, sizeof(adj));
int n, m;
scanf("%d%d", &n, &m);
T = n + 1;
for (int i = 0, x, y, z; i < m; i++)
scanf("%d%d%d", &x, &y, &z), addEdge(x, y, 1, z), addEdge(y, x, 1, z);
addEdge(0, 1, 2, 0);
addEdge(n, T, 2, 0);
int ans = 0;
while (bfs(0)) ans += dis[T] * dfs(0, inf);
printf("%d", ans);
return 0;
}

@ -485,8 +485,11 @@ Manacher 模板题
\subsection{最大流/最小割} 我不想抄讲义了就通俗易懂的说一下。每次找一条从源点到汇点的可行流增广直到源点和汇点不联通。就这样。Dinic和ISAP都是最短增广路算法不同的就是维护增广路的方式。
\paragraph{Dinic} 虽然我并不使用Dinic不过我认为研究这种算法也是有其必要性的。比如可以加深对费用流的理解滑稽
\codeinput[\href{http://poj.org/problem?id=1273}{POJ1273} - Drainage Ditches (Dinic)]{assets/day6/poj1273_dinic.cpp}
对这段代码简单的说两句。值得注意的有这么几点,一是\verb|cur[u]|的使用在单次增广时可以避免无谓的探查二是4041行在没有把以上配额用光之前没有返回可以造成多路增广的情况来增加效率三是第4243行的判断如果所有子节点的可用流之和都不能满足当前点的配额这个点就没有访问的必要了。我想大概就这么些。
\paragraph{ISAP} 我经常使用ISAP因为据说这玩意更稳定由于我的脸黑我怕碰上一些恶心的数据卡掉Dinic并且目前没有一道普通网络流的题能卡掉ISAP,我很高兴。以下还是上面那道题。
对这段代码简单的说两句。值得注意的有这么几点,一是\verb|cur[u]|的使用在单次增广时可以避免无谓的探查二是4041行在没有把以上配额用光之前没有返回可以造成多路增广的情况来增加效率三是第4243行的判断如果所有子节点的可用流之和都不能满足当前点的配额这个点就没有访问的必要了。我想大概就这么些。
\paragraph{ISAP} 我经常使用ISAP因为据说这玩意更稳定由于我的脸黑我怕碰上一些恶心的数据卡掉Dinic并且目前没有一道普通网络流的题能卡掉ISAP。以下还是上面那道题。
\codeinput[\href{http://poj.org/problem?id=1273}{POJ1273} - Drainage Ditches (ISAP)]{assets/day6/poj1273_ISAP.cpp}
重新写了一遍1A我还是很高兴。ISAP比Dinic要注意的点真是多多了注意第58行的break这可以大大加速因为此时源点和汇点已经不会联通了继续做下去无益。反正我觉得ISAP要是能一遍写出来的话是比Dinic好的。
重新写了一遍1A我很高兴。ISAP比Dinic要注意的点真是多多了注意第58行的break这可以大大加速因为此时源点和汇点已经不会联通了继续做下去无益。反正我觉得ISAP要是能一遍写出来的话是比Dinic好的。
\subsection{最小费用最大流}
每次找到一条最短的路增广大体上与Dinic完全相同不过总感觉很奇怪包括没有了cur多出了vis等一些不同并且不加就会爆炸。
\codeinput[\href{http://poj.org/problem?id=2135}{POJ2135} - Farm Tour]{assets/day6/poj2135.cpp}
\end{document}
Loading…
Cancel
Save