Drainage Ditches
Total Submission(s): 9062 Accepted Submission(s): 4254
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.
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
50 代码:#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;
#define inf 0x7fffffff
int map[300][300];
int maxf[300],pre[300];
int n,m;
int min(int x,int y)
{
return x<y?x:y;
}
int bfs()
{
queue<int>q;
int i,hou;
for(i=0;i<=m;i++)
{
pre[i]=-1;
maxf[i]=inf;
}
q.push(1);
pre[1]=0;
while(!q.empty())
{
int qian=q.front();
q.pop();
for(hou=1;hou<=m;hou++)
{
if(map[qian][hou]&&pre[hou]==-1)
{
pre[hou]=qian;
maxf[hou]=min(maxf[qian],map[qian][hou]);
q.push(hou);
}
}
}
if(maxf[m]==inf)
return 0;
return maxf[m];
}
int ek()
{
int max=0;
int index,qian,hou,kejia;
while(kejia=bfs())
{
max+=kejia;
index=m;
while(index!=1)
{
qian=pre[index];
map[qian][index]-=kejia;
map[index][qian]+=kejia;
index=qian;
}
}
return max;
}
int main()
{
int i,u,v,w;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&u,&v,&w);
map[u][v]+=w;
}
int ans=ek();
printf("%d\n",ans);
}
return 0;
}