题目链接 Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 16787 Accepted Submission(s): 7915
Problem Description
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)
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
http://v.youku.com/v_show/id_XMTY4MTgwMTYzNg==.html?spm=a2hzp.8244740.0.0 网络流的基本概念,了解一下思路即可,不需要看太懂,再看下面的代码应该不算很难懂的
Dinic模板代码:(约500ms)
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
int head[100000];
struct edge
{
int from,to,w,next;
}e[1000000];
int div[100000];
int n,m,cont,ss,tt;
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
int makediv()寻找增广路
{
memset(div,0,sizeof(div));
queue<int >s;
s.push(ss);
div[ss]=1;
while(!s.empty())
{
int u=s.front();
if(u==tt)return 1;
s.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(w&&div[v]==0)
{
div[v]=div[u]+1;
s.push(v);
}
}
}
return 0;
}
int Dfs(int u,int maxflow,int tt)//maxflow代表从u能流出总流量
{
if(u==tt)return maxflow;
int ret=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(w&&div[v]==div[u]+1)
{
int f=Dfs(v,min(maxflow-ret,w),tt);//f是v能到tt流量。 min(maxflow-ret(没有流到tt的剩下的流量),w(这条路最大能通过的流量))
e[i].w-=f;残余流量的更新,e[i].w与e[i^1].w就是同一条路,不同方向
e[i^1].w+=f;
ret+=f;//ret(maxflow总流量中的一部分)是已经流到tt的流量。
if(ret==maxflow) return ret;// maxflow都流到了tt处;
}
}
return ret;
}
void Dinic()
{
ss=1;tt=n;
int ans=0;
while(makediv()==1)//判断是否有增广路
{
ans+=Dfs(ss,INF,tt);//处理增广路
}
printf("%d\n",ans);
}
int main()
{
int t;
int kase=0;
scanf("%d",&t);
while(t--)
{
cont=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);//这个地方处理的感觉没有下边sap的代码好
add(y,x,0);
}
printf("Case %d: ",++kase);
Dinic();
}
}
https://wenku.baidu.com/view/c0b4915005087632311212a7.html (sap详解)
#include <stdio.h>
#include <string.h>
#define maxn 16
#define maxx 500
#define inf 0x3fffffff
#define MIN(a,b) a>b?b:a
#define mems(p) memset(p,0,sizeof(p))
struct E
{
int to,val,next;
}e[maxx];
int vs,S,T,cnt;
int g[maxn][maxn],dis[maxn],gap[maxn],head[maxn];
void add(int from,int to,int val)
{
e[cnt].to=to;
e[cnt].val=val;
e[cnt].next=head[from];
head[from]=cnt++;
e[cnt].to=from;
e[cnt].val=0;
e[cnt].next=head[to];
head[to]=cnt++;
}
int dfs(int s,int aug)
{
if(s==T) return aug;
int flow=0,mi=vs-1;
for(int i=head[s];i;i=e[i].next)
if(e[i].val)
{
if(dis[s]==dis[e[i].to]+1)
{
int t=dfs(e[i].to,MIN(aug-flow,e[i].val));
e[i].val-=t;
e[i^1].val+=t;
flow+=t;
if(dis[S]>=vs) return flow;
if(aug==flow)break;
}
mi=MIN(mi,dis[e[i].to]);
}
if(!flow)
{
if(!(--gap[dis[s]]))
dis[S]=vs;//这个地方我以前写错了
++gap[dis[s]=mi+1];
}
return flow;
}
int slove(int s,int ed)
{
mems(gap);
mems(dis);
gap[0]=vs=ed;
S=s;
T=ed;
int ans=0;
while(dis[S]<vs)
ans+=dfs(S,inf);
return ans;
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--)
{
mems(g);
mems(head);
cnt=2;
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
g[x][y]+=z;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(g[i][j])
add(i,j,g[i][j]);
printf("Case %d: %d\n",cas++,slove(1,n));
}
}