Cod sursa(job #453650)

Utilizator Andreid91Ciocan Andrei Andreid91 Data 11 mai 2010 10:27:51
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include<fstream.h>
#include<algorithm>
#include<vector>
#include<deque>

using namespace std;

int n,m,cap[1002][1002],flux[1002][1002],sw[1002],sol,t[1002];

vector <int > v[1002];

deque <int> C;

void afisare ()
{
	ofstream f("maxflow.out");
	f<<sol;
	f.close();
}

int getflux ()
{
	int i,j,fluxmax=0,max,p;
	
	deque <int> :: iterator it1;
	vector <int > :: iterator it;
	
	C.clear();
	C.push_back(1);
	memset(sw,0,sizeof(sw));
	sw[1]=1;
	
	while (C.begin()<C.end())
	{
		it1=C.begin();
		for (it=v[*it1].begin();it<v[*it1].end();++it)
			if (sw[*it]==0 && flux[*it1][*it]<cap[*it1][*it] && *it!=n)
			{
				C.push_back(*it);
				sw[*it]=1;
				t[*it]=*it1;
			}
		C.pop_front();
	}
	
	for (it=v[n].begin();it<v[n].end();++it)
	{
		max=cap[*it][n]-flux[*it][n];
		for (p=*it;p!=1;p=t[p])
			if (max>cap[t[p]][p]-flux[t[p]][p])
				max=cap[t[p]][p]-flux[t[p]][p];
		
		fluxmax+=max;
			
		if (max>0)
		{
			flux[*it][n]+=max;
			flux[n][*it]-=max;
			for (p=*it;p!=1;p=t[p])
			{
				flux[t[p]][p]+=max;
				flux[p][t[p]]-=max;
			}
		}
	}
	return fluxmax;
}

void flow ()
{
	int f=1;
	while (f>0)
	{
		f=getflux();
		sol+=f;
	}
}
	

void citire ()
{
	int i,x,y,z;
	ifstream f("maxflow.in");
	f>>n>>m;
	
	for (i=1;i<=m;i++)
	{
		f>>x>>y>>z;
		cap[x][y]=z;
		v[x].push_back(y);
		v[y].push_back(x);
	}

	f.close();
}		

int main()
{
	citire ();
	flow();
	afisare ();
	return 0;
}