Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 28193 Accepted Submission(s): 12476
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
题意:有t个测试样例,n个点,m条边组成的一个网络图,问从起点1到终点n的最大流量是多少
最大流模板题,EK算法
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#define ll long long
#define mx 0x3f3f3f3f
using namespace std;
int flow[][],cap[][];//flow当前流量,cap总容量
int f[],vis[];//f[i]最小残量=cap-flow,vis[i]标记i节点的上一个最小残量所在的位置
int mx_flow;//最大流量,所有增广路最小残量之和
void bfs(int n)
{
queue<int>p;
mx_flow=;
int flag=;
memset(flow,,sizeof(flow));
while(flag==)
{
memset(f,,sizeof(f));
memset(vis,,sizeof(vis));
f[]=mx,vis[]=-;//初始化源点
p.push();
while(!p.empty())//bfs找增广路
{
int now=p.front();
p.pop();
for(int i=;i<=n;i++)
{
if(!f[i]&&flow[now][i]<cap[now][i])
{
f[i]=min(f[now],cap[now][i]-flow[now][i]);//取最小残量
vis[i]=now;
p.push(i);
}
}
}
if(f[n]==)//容量-流量==0,一条增广路寻找结束
flag=;
mx_flow+=f[n];
int pos=n;//从汇点开始更新流量
while(!flag&&pos!=)
{
flow[vis[pos]][pos]+=f[n];//正向更新流量
flow[pos][vis[pos]]-=f[n];//反向更新流量
pos=vis[pos];
}
}
} int main()
{
int t,n,m,cnt=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);//n是顶点数,m是边数
memset(cap,,sizeof(cap));
for(int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
cap[x][y]+=z;
}
bfs(n);
printf("Case %d: %d\n",++cnt,mx_flow);
}
}