Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 64253 | Accepted: 24793 |
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
#define maxn 250
#define INF 0x3f3f3f3f
int flow[maxn][maxn],a[maxn],p[maxn];
// flow 是流量,cap是容量,a[i]表示源点s到节点i的路径上的最小残留量
int cap[maxn][maxn];
int min(int a,int b){
return a>b?b:a;
}
int m;
void EK(int st,int end){
int u,v,i;
int sum = 0;
queue<int > q;
memset(flow,0,sizeof(flow));
memset(p,0,sizeof(p));
while(1){
memset(a,0,sizeof(a));
a[st] = INF;
q.push(st);
while(!q.empty()){
u = q.front();
q.pop();
for(v=1;v<=m;v++){ // 没有连通的点flow[u][v]==cap[u][v]==0
if(!a[v] && flow[u][v]<cap[u][v]){ //每次a[i]都会被初始化为0,当不为0的时候,就相当于被标记过了
p[v] = u;
q.push(v);
a[v] = min(a[u],cap[u][v]-flow[u][v]); // u是v的前驱节点,用前驱和当前比较,取小的那一个
}
}
}
if(a[m]==0) // 找不到增广路,说明已经是最大流
break;
sum+=a[m]; //每一次都能找到一条增广路,只有一条哦,所以sum+=a[m],每次只加一次是可行的
for(i=m;i!=st;i=p[i]){
flow[p[i]][i] += a[m];
flow[i][p[i]] -= a[m]; // 将来作比较的时候,用cap[u][v](==0) - flow[u][v](<0) 得到的仍然是一个正数。
//后向弧为算法纠正自己所犯的错误提供了可能性,它允许算法取消先前的错误的行为
}
}
printf("%d\n",sum);
}
int main(){
int i,j,k,t,n;
int st,end,w;
while(scanf("%d%d",&n,&m)!=EOF){
memset(cap,0,sizeof(cap));
while(n--){
scanf("%d%d%d",&st,&end,&w);
cap[st][end] += w;
}
EK(1,m); // 分别是源点和汇点
}
return 0;
}