Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 7863 Accepted Submission(s): 3657
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
<span style="font-size:18px;">#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
using namespace std;
int flow[1010][1010];//记录流量
int liu[1010];//记录可以流过去的量
int vis[1010];
int father[1010];//记录上一个状态
int max_flow;
void Edmondes_Karp(int s,int t)
{
queue<int >p;
int x,y,i,j;
while(1)
{
memset(vis,0,sizeof(vis));
memset(liu,0,sizeof(liu));
liu[s]=100000000;//源点的流入量置为正无穷
p.push(s);//将源点压入队列
while(!p.empty())
{
x=p.front();
p.pop();
for(i=s; i<=t; i++)
{
if(vis[i]==0&&flow[x][i]>0)
{
vis[i]=1;
father[i]=x;//将上一点保存下来
p.push(i);
liu[i]=min(liu[x],flow[x][i]);//以最小的流量为准
}
}
}
if(liu[t]==0)//证明没有其它的增广路可达
break;
for(j=t; j!=s; j=father[j])
{
flow[father[j]][j]-=liu[t];//正向更新
flow[j][father[j]]+=liu[t];//反向更新(刚开始时为0)
}
max_flow+=liu[t];//将每一次的流量加起来
}
}
int main()
{
int n,m,i,a,b,c,T;
scanf("%d",&T);
int num=1;
while(T--)
{
scanf("%d%d",&n,&m);
max_flow=0;
memset(flow,0,sizeof(flow));
for(i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
flow[a][b]+=c;
}
Edmondes_Karp(1,n);
printf("Case %d: %d\n",num++,max_flow);
}
return 0;
}
</span>