Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 70197 Accepted: 27270
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
水一下,涨熟练度
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
//标号法
const int maxn=205;
const int INF=0x3f3f3f3f;
struct node
{
int c;//容量
int f;//流量
} map[maxn][maxn];
int vis[maxn];//
int path[maxn];
int alpha[maxn];
int N,M;
int Find_Path()//返回可改进量
{
memset(vis,0,sizeof(vis));
memset(path,0,sizeof(path));
memset(alpha,0,sizeof(alpha));
vis[1]=1;
path[1]=1;
alpha[1]=INF;//源点流出流量无数
queue<int>q;
q.push(1);
while(!q.empty())
{
int star=q.front();
q.pop();
for(int i=1; i<=N; i++)
{
if(vis[i]==0)
{
if(map[star][i].c-map[star][i].f>0)
{
path[i]=star;
alpha[i]=min(map[star][i].c-map[star][i].f,alpha[star]);
vis[i]=1;
q.push(i);
}
else if(map[i][star].f>0)//反向且有流量
{
path[i]=star;
alpha[i]=min(map[i][star].f,alpha[star]);
vis[i]=1;
q.push(i);
}
}
}
}
return alpha[N];
}
void Update_Path()//改进网络
{
for(int i=N; i!=1; i=path[i])
{
if(map[path[i]][i].c-map[path[i]][i].f>0)
map[path[i]][i].f+=alpha[N];
else if(map[i][path[i]].f>0)
map[i][path[i]].f-=alpha[N];
}
}
int main()
{
while(~scanf("%d%d",&M,&N))
{
memset(map,0,sizeof(map));
//建图
while(M--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
map[u][v].c+=w;
}
int max_flow=0;//网络最大流
while(1)
{
int flag_flow=Find_Path();
if(flag_flow==0)
break;
max_flow+=flag_flow;
Update_Path();
}
printf("%d\n",max_flow);
}
return 0;
}