题目:
http://poj.org/problem?id=3469
实测发现,当前弧、多路增广和当前弧+多路增广这三种优化其实差不多。。。但还是留个模板吧
只有当前弧:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 20010;
const int INF = 0x3f3f3f3f;
struct edge
{
int to, cap, next;
} g[N*50];
int head[N], iter[N], level[N];
int cnt;
void add_edge(int v, int u, int cap)
{
g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
bool bfs(int s, int t)
{
memset(level, -1, sizeof level);
queue<int> que;
level[s] = 0;
que.push(s);
while(! que.empty())
{
int v = que.front();
que.pop();
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[u] < 0)
{
level[u] = level[v] + 1;
que.push(u);
}
}
}
return level[t] == -1;
}
int dfs(int v, int t, int f)
{
if(v == t) return f;
for(int &i = iter[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[v] < level[u])
{
int d = dfs(u, t, min(f, g[i].cap));
if(d > 0)
{
g[i].cap -= d, g[i^1].cap += d;
return d;
}
}
}
return 0;
}
int dinic(int s, int t)
{
int flow = 0, f;
while(true)
{
if(bfs(s, t)) return flow;
memcpy(iter, head, sizeof head);
while(f = dfs(s, t, INF), f > 0)
flow += f;
}
}
int main()
{
int n, m, a, b, c;
while(~ scanf("%d%d", &n, &m))
{
cnt = 0;
memset(head, -1, sizeof head);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &a, &b);
add_edge(0, i, a);
add_edge(i, n + 1, b);
}
for(int i = 0; i < m; i++)
{
scanf("%d%d%d", &a, &b, &c);
add_edge(a, b, c);
add_edge(b, a, c);
}
printf("%d\n", dinic(0, n + 1));
}
return 0;
}
当前弧+多路增广:
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <vector>
#include <queue>
using namespace std;
const int N = 20100;
const int INF = 0x3f3f3f3f;
struct edge
{
int to, cap, next;
}g[N*100];
int level[N], iter[N], head[N], que[N];
int n, m, cnt;
void add_edge(int v, int u, int cap)
{
g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
bool bfs(int s, int t)
{
memset(level, -1, sizeof level);
level[s] = 0;
int st = 0, en = 0;
que[0] = s;
while(st <= en)
{
int v = que[st++];
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[u] < 0)
{
level[u] = level[v] + 1;
que[++en] = u;
}
}
}
return level[t] == -1;
}
//下面两个dfs都是当前弧+多路增广
int dfs(int v, int t, int f)
{
if(v == t) return f;
int num = 0;
for(int &i = iter[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[v] < level[u])
{
int d = dfs(u, t, min(f - num, g[i].cap));
g[i].cap -= d, g[i^1].cap += d, num += d;
if(num >= f) break;
}
}
if(! num) level[v] = -1;
return num;
}
int dfs(int v, int t, int f)
{
if(v == t) return f;
int tm = f;
for(int &i = iter[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(g[i].cap > 0 && level[v] < level[u])
{
int d = dfs(u, t, min(tm, g[i].cap));
g[i].cap -= d, g[i^1].cap += d, tm -= d;
if(tm == 0) break;
}
}
if(tm == f) level[v] = -1;
return f - tm;
}
int dinic(int s, int t)
{
int flow = 0, f;
while(true)
{
if(bfs(s, t)) return flow;
memcpy(iter, head, sizeof head);
flow += dfs(s, t, INF);
}
}
int main()
{
int a, b, c;
while(~ scanf("%d%d", &n, &m))
{
cnt = 0;
memset(head, -1, sizeof head);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &a, &b);
add_edge(0, i, a);
add_edge(i, n + 1, b);
}
for(int i = 0; i < m; i++)
{
scanf("%d%d%d", &a, &b, &c);
add_edge(a, b, c);
add_edge(b, a, c);
}
printf("%d\n", dinic(0, n + 1));
}
return 0;
}