Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20826 Accepted Submission(s): 9983
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找一条最短的增广路径,然后沿着这条路径修改流量值(实际修改的是残量网络的边权),当没有增广路时,算法停止,此时的流就是最大流。
网络流有三个基本概念要掌握:
1、容量限制: f[u,v]<=c[u,v](当前路径的流量必须小于这条路径的最大容量)
2、反对称性:f[u,v] = - f[v,u](当有向边u到v相通而v到u不通时,从u到v的流量和从v到u的流量的绝对值相等但是相反数)
3、流量平衡: 对于不是源点也不是汇点的任意结点,流入该结点的流量和等于流出该结点的流量和。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int N,M;
int s,d;
int cap[205][205];//表示网络的最大容量
int flow[205][205];//当前网络的流网络
int pre[205];//用于表示路径
int minflow[205];//用于表示该条路径中每个路径的最小残留量
void Edmonds_Karp()
{
int u;
int ans=0;
queue<int> Q;
while(true)
{
Q.push(s);//原点进队,从原点开始找到终点的路径
memset(minflow,0,sizeof(minflow));
minflow[s]=0x3f3f3f;
while(!Q.empty())
{
u=Q.front();
Q.pop();
for(int v=1;v<=M;v++)
{
if(minflow[v]==0&&flow[u][v]<cap[u][v])//如果在此查找路径中,该条路径没有被查找过,且该条路径可通,且该条路径还存在残留量
{
pre[v]=u;//标记此路
Q.push(v);
minflow[v]=min(minflow[u],cap[u][v]-flow[u][v]);//该条路径的最小残留量和之前路径的最小残留量相比找出整条路径的最小残留量
}
}
}
if(minflow[d]==0)
break;
ans+=minflow[d];//minflow[d]表示当前这条到达汇点的路径的最小残留量(即最大的增广的流量)。
for(int v=d;v!=s;v=pre[v])
{
//对流网络中当前遍历的路径加上最小残留量
int u=pre[v];
flow[u][v]+=minflow[d];
flow[v][u]-=minflow[d];//反对称性
}
}
printf("%d\n",ans);
}
int main()
{
int u,v,w;
while(scanf("%d%d",&N,&M)!=EOF)
{
s=1;
d=M;
memset(cap,0,sizeof(cap));
memset(flow,0,sizeof(flow));
for(int i=1;i<=N;i++)
{
scanf("%d%d%d",&u,&v,&w);
cap[u][v]+=w;
}
Edmonds_Karp();
}
return 0;
}