Cod sursa(job #914665)

Utilizator dragangabrielDragan Andrei Gabriel dragangabriel Data 14 martie 2013 12:38:21
Problema Flux maxim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 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];
bool viz[nmax];
deque<int>c;

bool 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]])
			viz[v[x][j]]=true,din[v[x][j]]=x,c.push_back(v[x][j]);
		c.pop_front();
	}
	if (!viz[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);
	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;
			x=din[x];
		}
		memset(din,0,sizeof(din));
		memset(viz,false,sizeof(viz));
	}
	printf("%d\n",rez);
	return 0;
}