Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 49853 | Accepted: 18918 |
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
灰常经典的最大流,我是用的dinic算法,我们可以看出这个算法,是很快的,先用bfs,再用dfs,这样就非常好的效果!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define typec int
const typec inf=0x3f3f3f3f;
#define E 402
#define N 200
struct edge{int x,y,nxt;typec c;}bf[E];
int ne,head[N],cur[N],ps[N],dp[N],dep[N];
void addedge(int x,int y,typec c)
{
bf[ne].x=x;bf[ne].y=y;bf[ne].c=c;
bf[ne].nxt=head[x];head[x]=ne++;
bf[ne].x=y;bf[ne].y=x;bf[ne].c=0;
bf[ne].nxt=head[y];head[y]=ne++;
}
typec flow(int n,int s,int t)
{
typec tr,res=0;
int i,j,k,f,r,top;
while(1)
{
memset(dep,-1,n*sizeof(int));
for(f=dep[ps[0]=s]=0,r=1;f!=r;)
{
for(i=ps[f++],j=head[i];j;j=bf[j].nxt)
{
if(bf[j].c&&-1==dep[k=bf[j].y])
{
dep[k]=dep[i]+1;ps[r++]=k;
if(k==t)
{
f=r;break;
}
}
}
}
if(-1==dep[t])break;
memcpy(cur,head,n*sizeof(int));
for(i=s,top=0;;)
{
if(i==t)
{
for(k=0,tr=inf;k<top;++k)
{
if(bf[ps[k]].c<tr)
tr=bf[ps[f=k]].c;
}
for(k=0;k<top;k++)
{
bf[ps[k]].c-=tr,bf[ps[k]^1].c+=tr;
}
res+=tr;i=bf[ps[top=f]].x;
}
for(j=cur[i];cur[i];j=cur[i]=bf[cur[i]].nxt)
if(bf[j].c&&dep[i]+1==dep[bf[j].y])break;
if(cur[i])
{
ps[top++]=cur[i];
i=bf[cur[i]].y;
}
else
{
if(0==top)break;
dep[i]=-1;i=bf[ps[--top]].x;
}
}
}
return res;
}
int main()
{
int n,m,i,s,e,val;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,0,sizeof(head));
ne=2;
for(i=0;i<n;i++)
{
scanf("%d%d%d",&s,&e,&val);
s--,e--;
addedge(s,e,val);
}
printf("%d\n",flow(m,0,m-1));
}
return 0;
}