Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 7288 Accepted Submission(s): 3379
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
Author
HyperHexagon
Source
Recommend
思路: 测isap板子。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1000+100;
const int inf=0x3fffffff;
int n,m,edge_cnt,top;
int que[MAXN],s[MAXN];
int head[MAXN],depth[MAXN],cur[MAXN],gap[MAXN];
struct Edge
{
int v,cap,flow,next;
}edge[2*MAXN];
void init()
{
edge_cnt=top=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
edge[edge_cnt].v=v;
edge[edge_cnt].cap=w; edge[edge_cnt].flow=0;
edge[edge_cnt].next=head[u]; head[u]=edge_cnt++;
}
void bfs(int start,int end)
{
memset(depth,-1,sizeof(depth));
memset(gap,0,sizeof(gap));
int front=0,rear=0; gap[0]=1;
depth[end]=0; que[rear++]=end;
while(front!=rear){
int u=que[front++];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(depth[v]!=-1) continue;
que[rear++]=v;
depth[v]=depth[u]+1; gap[depth[v]]++;
}
}
}
int isap(int start,int end)
{
bfs(start,end);
memcpy(cur,head,sizeof(head));
top=0;
int ans=0,u=start;
while(depth[start]<n){
if(u == end){
int Min=inf,inser;
for(int i=0;i<top;i++)
if(edge[s[i]].cap-edge[s[i]].flow < Min){
Min=edge[s[i]].cap-edge[s[i]].flow;
inser=i;
}
for(int i=0;i<top;i++){
edge[s[i]].flow+=Min; edge[s[i]^1].flow-=Min;
}
ans+=Min; top=inser;
u=edge[s[top]^1].v;
continue;
}
int flag = 0;
int v;
for(int i=cur[u];i!=-1;i=edge[i].next){
v = edge[i].v;
if(edge[i].cap-edge[i].flow && depth[v]+1 == depth[u]){
flag=1; cur[u]=i;
break;
}
}
if(flag){
s[top++] = cur[u]; u=v;
continue;
}
int d=n;
for(int i=head[u];i!=-1;i= edge[i].next)
if(edge[i].cap-edge[i].flow && depth[edge[i].v]<d)
{
d=depth[edge[i].v];
cur[u] = i;
}
gap[depth[u]]--;
if(!gap[depth[u]])return ans;
depth[u]=d+1; gap[depth[u]]++;
if(u!=start) u=edge[s[--top]^1].v;
}
return ans;
}
int main()
{
//freopen("text.txt","r",stdin);
int T,kase=0;
scanf("%d",&T);
while(T--){
kase++;
init();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w); addedge(v,u,0);
}
printf("Case %d: %d\n",kase,isap(1,n));
}
return 0;
}