poi 1273

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

int map[205][205];
int flag[205];
int flow[205];
int n,m;
const int M = 0x7FFFFFFF;
int start;
//int ends;
queue<int> g;

inline int minx(int a,int b)
{
	return a>b?b:a;
}
int bfs()
{
	int i,j;
    memset(flag,-1,sizeof(flag));
	while(!g.empty())g.pop();

	flag[start] = 0;
	flow[start] = M;
	g.push(start);
	int ends = m;

	while(!g.empty())
	{
		i = g.front();
		g.pop();

		if(i==ends)break;

		for(int j=1;j<=m;j++)
			if(j!=start&&flag[j]==-1&&map[i][j])
			{
				flow[j]=minx(flow[i],map[i][j]);
				flag[j] = i;
				g.push(j);
			}
	}

	if(flag[ends]==-1)return -1;
	return flow[ends];
}
int solve()
{
	int pre,now;
	int maxflow = 0;
	while((pre=bfs())!=-1)
	{
		maxflow += pre;
		now = m;
		while(now!=start)
		{
			map[flag[now]][now]-=pre;
			map[now][flag[now]] += pre;
			now = flag[now];
		}
	}
	return maxflow;
}
int main()
{
	int a,b,c;
	while(cin>>n>>m)
	{
		memset(map,0,sizeof(map));
		for(int i=0;i<n;i++)
		{
			cin>>a>>b>>c;
			map[a][b]+=c;
		}
		start = 1;//ends = m;
		cout<<solve()<<endl;
	}
   return 0;
}

你可能感兴趣的:(poi 1273)