[codevs 1993]草地排水

#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 205
using namespace std;
int n,m;
int flow[maxn][maxn],cap[maxn][maxn],d[maxn],father[maxn];
queue<int> q;
int ans;
int main()
{
	scanf("%d%d",&n,&m);
	int s,e,c;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d",&s,&e,&c);
		cap[s][e]+=c;
	}
	for(;;)
	{
		memset(d,0,sizeof(d));
		q.push(1);
		d[1]=1e9;
		while(!q.empty())
		{
			int p=q.front();
			q.pop();
			for(int i=1;i<=m;i++)
			{
				if(!d[i]&&cap[p][i]>flow[p][i])
				{
					father[i]=p;
					d[i]=min(d[p],cap[p][i]-flow[p][i]);
					q.push(i);
				}
			}
		}
		if(d[m]==0)break;
		for(int i=m;i!=1;i=father[i])
     	{
            flow[father[i]][i]+=d[m];
            flow[i][father[i]]-=d[m];
     	}
    	 ans+=d[m];
	}
	printf("%d",ans);
	return 0;
}

你可能感兴趣的:(图论,网络流)