Cod sursa(job #914848)

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

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

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),v[y].push_back(x);
	int ok=1;
	while (df(1))
	{
		minim=2000000000;x=n;
		while (x!=1)
		{
			minim=min(minim,mat[din[x]][x]);
			x=din[x];
		}
		rez+=minim;x=n;
		while (x!=1)
		{
			mat[din[x]][x]-=minim;
			mat[x][din[x]]+=minim;
			x=din[x];
		}
		memset(din,0,sizeof(din));
	}
	printf("%d\n",rez);
	return 0;
}