Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
Input
The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
Output
For each test cases, you should output the maximum flow from source 1 to sink N.
Sample Input
2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
Sample Output
Case 1: 1 Case 2: 2
求1-n的最大流
第一种方法 EK动能算法
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
struct edge {
int v, w, next;
} e[200005];
struct node {
int v, e;
} p[10005];
int head[10005], vis[10005];
int n, m, s, t, cnt = 1;
void addedge(int u, int v, int w) {
e[++cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt;
}
bool bfs() {
queue<int> q;
memset(p, 0, sizeof(p));
memset(vis, 0, sizeof(vis));
vis[s] = 1;
q.push(s);
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = head[cur]; i; i = e[i].next)
if ((!vis[e[i].v]) && e[i].w) {
p[e[i].v].v = cur;
p[e[i].v].e = i;
if (e[i].v == t) return 1;
vis[e[i].v] = 1;
q.push(e[i].v);
}
}
return 0;
}
int main() {
int T;scanf("%d",&T);
for(int cc=1;cc<=T;cc++)
{
memset(head,0,sizeof head);
memset(p,0,sizeof p);cnt=1;
memset(e,0,sizeof e);
scanf("%d%d", &n, &m);s=1,t=n;
for (int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, 0);
}
int ans = 0;
while (bfs()) {
int minw = INF;
for (int i = t; i != s; i = p[i].v) minw = min(minw, e[p[i].e].w);
for (int i = t; i != s; i = p[i].v) {
e[p[i].e].w -= minw;
e[p[i].e ^ 1].w += minw;
}
ans += minw;
}
printf("Case %d: %d\n",cc,ans);
}
return 0;
}
第二种 dinic算法
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
struct edge {
int v, w, next;
} e[2005];
int n, m, s, t, cnt = 1;
int head[1000], dep[1000], vis[1000], cur[1000];
void addedge(int u, int v, int w) {
e[++cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt;
}
bool bfs() {
queue<int> q;
memset(dep, INF, sizeof(dep));
memset(vis, 0, sizeof(vis));
memcpy(cur, head, sizeof(head));
dep[s] = 0;
vis[s] = 1;
q.push(s);
while (!q.empty()) {
int p = q.front();
q.pop();
vis[p] = 0;
for (int i = head[p]; i; i = e[i].next)
if (dep[e[i].v] > dep[p] + 1 && e[i].w) {
dep[e[i].v] = dep[p] + 1;
if (!vis[e[i].v]) {
vis[e[i].v] = 1;
q.push(e[i].v);
}
}
}
if (dep[t] == INF) return 0;
return 1;
}
int dfs(int p, int w) {
if (p == t) return w;
int used = 0; //已经使用的流量
for (int i = cur[p]; i; i = e[i].next) //每条边都尝试找一次增广路
{
cur[p] = i; //当前弧优化
if (dep[e[i].v] == dep[p] + 1 && e[i].w) {
int flow = dfs(e[i].v, min(w - used, e[i].w));
if (flow) {
used += flow;
e[i].w -= flow;
e[i ^ 1].w += flow;
if (used == w) break; //残余流量用尽了就停止增广
}
}
}
return used;
}
int main()
{
int T;scanf("%d",&T);
for(int cc=1;cc<=T;cc++)
{
memset(head,0,sizeof head);
memset(e,0,sizeof e); cnt=1;
scanf("%d%d", &n, &m);s=1,t=n;
for (int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, 0);
}
int ans = 0;
while (bfs()) ans += dfs(s, INF);
printf("Case %d: %d\n",cc, ans);
}
return 0;
}
第三种 ISAP算法
#include<cstdio>
#include<cctype>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=205;
const int maxm=2050;
const int inf=2e9+7;
struct edge {
int v,w,nxt;
} e[maxm<<1];
int h[maxn],tot,n,m,gap[maxn],last[maxn],d[maxn],que[maxn],ql,qr;
vector<int> inv[maxn];
void add(int u,int v,int w) {
e[++tot]=(edge){v,w,h[u]};
h[u]=tot;
e[++tot]=(edge){u,0,h[v]};
h[v]=tot;
}
void init(int s,int t) {
memset(gap,0,sizeof gap),memset(d,0,sizeof d),++gap[d[t]=1];
for (int i=1;i<=n;++i) last[i]=h[i];
que[ql=qr=1]=t;
while (ql<=qr) {
int x=que[ql++];
for (int i=h[x],v=e[i].v;i;i=e[i].nxt,v=e[i].v) if (!d[v]) ++gap[d[v]=d[x]+1],que[++qr]=v;
}
}
int aug(int x,int s,int t,int mi) {
if (x==t) return mi;
int flow=0;
for (int &i=last[x],v=e[i].v;i;i=e[i].nxt,v=e[i].v) if (d[x]==d[v]+1) {
int tmp=aug(v,s,t,min(mi,e[i].w));
flow+=tmp,mi-=tmp,e[i].w-=tmp,e[i^1].w+=tmp;
if (!mi) return flow;
}
if (!(--gap[d[x]])) d[s]=n+1;
++gap[++d[x]],last[x]=h[x];
return flow;
}
int maxflow(int s,int t) {
init(s,t);
int ret=aug(s,s,t,inf);
while (d[s]<=n) ret+=aug(s,s,t,inf);
return ret;
}
int main(){
int T;scanf("%d",&T);
for(int cc=1;cc<=T;cc++)
{
scanf("%d%d",&n,&m);
tot=1,memset(h,0,sizeof h);
for (int i=1;i<=n;++i) inv[i].clear();
for (int i=1;i<=m;++i) {
int u=read(),v=read(),w=read();
add(u,v,w);
if (w) inv[v].push_back(u);
}
int ans=maxflow(1,n);
printf("Case %d: %d\n",cc,ans);
}
return 0;
}