Cod sursa(job #890107)

Utilizator tibi9876Marin Tiberiu tibi9876 Data 24 februarie 2013 21:04:08
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include<fstream>
#include<algorithm>
#include<vector>
#include<ctime>
#include<cstdlib>
using namespace std;

struct muchie
{
	int x,y,c,f;
};

muchie v[10005];
vector <int> a[1001];
int e[1001],d[1001],t[1001],i,j,n,m,x,y,z,p,u,k,F;
bool ok;

int main()
{
	ifstream f("maxflow.in");
	ofstream g("maxflow.out");
	srand(time(NULL));
	f >> n >> m;
	for (i=1;i<=m;i++)
	{
		f >> x >> y >> z;
		v[i].x=x;
		v[i].y=y;
		v[i].c=z;
		j=2*m-i+1;
		v[j].x=y;
		v[j].y=x;
		v[j].c=0;
		a[x].push_back(i);
		a[y].push_back(j);
	}
	m=m*2+1;
	ok=0;e[1]=1;
	while (ok==0)
	{
		ok=1;
		p=1;u=1;
		d[1]=1;t[1]=-1;
		for (i=2;i<=n;i++)
			e[i]=0;
		while (p<=u)
		{
			x=rand()%(u-p+1);
			x+=p;
			for (i=0;i<a[d[x]].size();i++)
			{
				y=a[d[x]][i];
				if ((e[v[y].y]==0) && (v[y].c-v[y].f>0))
				{
					d[++u]=v[y].y;
					e[d[u]]=1;
					t[d[u]]=y;
					if (d[u]==n)
						break;
				}
			}
			p++;
			if (d[u]==n)
			{
				ok=0;
				break;
			}
			
		}
		if (ok==0)
		{
			F=200000;
			x=n;
			while (t[x]>-1)
			{
				F=min(F,v[t[x]].c-v[t[x]].f);
				x=v[t[x]].x;
			}
			x=n;
			while (t[x]>-1)
			{
				v[t[x]].f+=F;
				v[m-t[x]].f-=F;
				x=v[t[x]].x;
			}
			k+=F;
		}
	}
	g << k;
	return 0;
}