Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 5984 Accepted Submission(s): 2786
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最大流模版题,Edmonds-Karp算法while(存在增广路){找到任意一条增广路;更新残余网络;}AC代码:#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdlib> #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) #define ll long long #define eps 1e-6 using namespace std; const int INF=1000000000; int c[20][20],f[20][20]; int n,m; int bfs() { queue<int>Q; int a[20],pre[20]; int ans=0; memset(f,0,sizeof(f)); while(1) { memset(a,0,sizeof(a)); a[1]=INF; Q.push(1); while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int v=1; v<=n; v++) if(!a[v]&&c[u][v]>f[u][v]) { pre[v]=u; Q.push(v); a[v]=min(a[u],c[u][v]-f[u][v]); } } if(!a[n]) break; for(int u=n;u!=1;u=pre[u]) { f[pre[u]][u]+=a[n]; f[u][pre[u]]-=a[n]; } ans+=a[n]; } return ans; } int main() { int t,ca=0; int x,y,z; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(c,0,sizeof(c)); while(m--) { scanf("%d%d%d",&x,&y,&z); c[x][y]+=z; } printf("Case %d: %d\n",++ca,bfs()); } return 0; }

本文介绍了一道典型的最大流问题,并提供了使用Edmonds-Karp算法求解的详细步骤及AC代码。该问题要求针对给定的加权有向图找出从源点到汇点的最大流量。
5423

被折叠的 条评论
为什么被折叠?



