Problem Description
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
Source
裸的最大流试模板,增广路dfs版和bfs版
DFS版:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int N = 405;//边数的两倍
const int inf = 1<<29;
struct Edge
{
int to;//终点
int cap;//容量
int rev;//反向边
Edge(int to = 0,int cap = 0,int rev = 0):to(to),cap(cap),rev(rev){}
};
vector<Edge>v[N];
bool used[N];
void add_Edge(int from,int to,int cap)//重边情况不影响
{
v[from].push_back(Edge(to,cap,v[to].size()));
v[to].push_back(Edge(from,0,v[from].size()-1));
}
int dfs(int s,int t,int f)
{
if (s == t) return f;
used[s] = 1;
for (int i = 0;i < v[s].size();++i)
{
Edge &tmp = v[s][i];//引用
if (!used[tmp.to]&&tmp.cap > 0)
{
int d = dfs(tmp.to,t,min(f,tmp.cap));
if (d > 0)
{
tmp.cap -= d;
v[tmp.to][tmp.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)//求源点汇点的最大流
{
int flow = 0;
while (1) //一直循环直到找不到增广路
{
mem(used,0);
int f = dfs(s,t,inf);
if (f == 0) return flow;
flow += f;
}
}
int main()
{
int n,m;
while (~scanf("%d %d",&n,&m))//n个边m个点
{
mem(v,0);
for (int i = 0,from,to,cap;i < n;++i)
{
scanf("%d %d %d",&from,&to,&cap);
add_Edge(from,to,cap);
}
printf("%d\n",max_flow(1,m));//这题源点为1,汇点为m
}
return 0;
}
BFS版:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
const int inf = 1 << 29;
const int N = 405;
struct Edge
{
int from, to, cap, flow;
Edge (int u = 0, int v = 0, int c = 0, int f = 0) : from (u), to (v), cap (c), flow (f) {}
};
struct EdmondsKarp
{
int n, m;
vector<Edge>edges;//边数的两倍
vector<int>G[N];//邻接表,G[i][j]表示节点i的第j条边在e数组中的序号
int a[N];//当起点到i的可改进量
int p[N];//最短路树上p的入弧编号
void init (int n)
{
for (int i = 0; i <= n; ++i) G[i].clear();
edges.clear();
}
void AddEdge (int from, int to, int cap)
{
edges.push_back (Edge (from, to, cap, 0) );
edges.push_back (Edge (to, from, 0, 0) ); //反向弧
m = edges.size();
G[from].push_back (m - 2);
G[to].push_back (m - 1);
}
int MaxFlow (int s, int t)
{
int flow = 0;
while (1)
{
mem (a, 0);
queue<int>Q;
Q.push (s);
a[s] = inf;
while (!Q.empty() )
{
int x = Q.front();
Q.pop();
for (int i = 0; i < G[x].size(); ++i)
{
Edge & e = edges[G[x][i]];
if (!a[e.to] && e.cap > e.flow)
{
p[e.to] = G[x][i];
a[e.to] = min (a[x], e.cap - e.flow);
Q.push (e.to);
}
}
if (a[t]) break;
}
if (!a[t]) break;
for (int u = t; u != s; u = edges[p[u]].from)
{
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
}
flow += a[t];
}
return flow;
}
};
int main()
{
int n, m;
while (~scanf ("%d %d", &m, &n) ) //n个点m条边
{
EdmondsKarp ans;
ans.init(n);
for (int i = 0,from,to,cap; i < m; ++i)
{
scanf("%d %d %d",&from,&to,&cap);
ans.AddEdge(from,to,cap);
}
printf("%d\n",ans.MaxFlow(1,n));
}
return 0;
}
/*
每条弧和对应的反向弧保存在一起
边0和1互为反向边,边2和3互为反向边......
一般地,边i的反向边为i^1
*/