Cod sursa(job #914632)

Utilizator dragangabrielDragan Andrei Gabriel dragangabriel Data 14 martie 2013 12:24:22
Problema Flux maxim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<deque>
#define nmax 1000+5
using namespace std;
int din[nmax],mat[nmax][nmax],m,n,i,j,k,x,y,flux[nmax],rez;
vector<int>v[nmax];
bool viz[nmax];
deque<int>c;

int df(int x)
{
	viz[x]=true;
	c.push_back(x);
	while (!c.empty())
	{
		x=c.front();
		for (j=0;j<v[x].size();j++) if (mat[x][v[x][j]] && !viz[v[x][j]])
		{
			y=v[x][j];
			viz[y]=true;
			din[y]=x;
			c.push_back(y);
			if (!flux[x]) flux[y]=mat[x][y];else
				flux[y]=min(flux[x],mat[x][y]);
		}
		c.pop_front();
	}
	return flux[n];
}

int main()
{
	freopen("maxflow.in","r",stdin);
	freopen("maxflow.out","w",stdout);
	scanf("%d %d",&n,&m);
	for (i=1;i<=m;i++) scanf("%d %d %d",&x,&y,&k),mat[x][y]=k,v[x].push_back(y);
	int ok=1;
	while (df(1)>0)
	{
		rez+=flux[n];
		x=n;
		while (x)
		{
			mat[din[x]][x]-=flux[n];
			x=din[x];
		}
		memset(din,0,sizeof(din));
		memset(flux,0,sizeof(flux));
		memset(viz,false,sizeof(viz));
	}
	printf("%d\n",rez);
	return 0;
}