Drainage Ditches
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.
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
解题思路
典型的最大流问题的模板题,此题要注意可能会出现循环路径,因此我们在进行bfs时候要标记该点是否已经走过了,如果已经走过那么我们的该条增广路径不再添加该点。
解题代码
#include <iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#define rep(i,s,e) for(int i =s ;i<e;i++)
using namespace std;
const int maxn = 300;
const int INF = 2000000;
int t;
int n,m;
struct Edge{
int from , to , cap,flow;
Edge(int u,int v,int c,int f): from(u),to(v),cap(c),flow(f){}
};
//增光路算法
struct EdomdsKarp{
int n,m;
vector<Edge> edges;
vector<int> G[maxn];//邻接表存储图
int a[maxn];//起点到i的可以改进量
int p[maxn];//最短路树上p的入弧编号
int vis[maxn];//判断是否回流
void init(int n){
rep(i,0,n) G[i].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){
long long flow = 0 ;
while(1){
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(s);
a[s] = INF;
while(!q.empty()){
int x = q.front(); q.pop();
vis[x] = 1;
int len = G[x].size();
rep(i,0,len){
Edge& e = edges[G[x][i]];
if(!vis[e.to]&&!a[e.to]&&e.cap > e.flow){
vis[e.to] = 1;
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(cin>>m>>n){
EdomdsKarp ek;
ek.init(n);
rep(i,0,m){
int u,v,c;
cin>>u>>v>>c;
ek.AddEdge(u-1,v-1,c);
}
cout<<ek.maxFlow(0,n-1)<<endl;
//if(t!=0) cout<<endl;
}
}