Cod sursa(job #743032)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 2 mai 2012 21:59:10
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include<vector>
#include<cstring>
#include<cstdio>

using namespace std;

const int maxn = 1 << 10;
const int inf = 0x3f3f3f3f;

int c[maxn][maxn], f[maxn][maxn], tt[maxn], m, n, cd[maxn], viz[maxn];

vector <int> g[maxn];

bool BF()
{
	int i, j, nod, V;
	
	cd[0]=1;
	cd[1]=1;
	memset(viz, 0, sizeof(viz));
	viz[1]=1;
	
	for(i=1; i<=cd[0]; ++i){
		nod = cd[i];
		if(nod == n)
			continue;
		
		for(j=0; j<g[nod].size(); ++j){
			V = g[nod][j];
			if(c[nod][V] == f[nod][V] || viz[V]) 
				continue;
			viz[V] = 1;
			cd[ ++cd[0] ] = V;
			tt[V] = nod;
		}
	}
	
	return viz[n];
}

int main()
{
	freopen("maxflow.in", "r", stdin);
	freopen("maxflow.out", "w", stdout);
	
	int i, flow, fmin, x, y, z, nod;
	
	//in >> n >> m;
	scanf("%d %d", &n, &m);
	
	for(i=1; i<=m; ++i){
		//in >> x >> y >> z;
		scanf("%d %d %d", &x, &y, &z);
		c[x][y]+=z;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	
	for(flow=0; BF();)
		for(i=0; i<g[n].size(); ++i){
			nod = g[n][i];
			if(c[nod][n] == f[nod][n] || !viz[nod])
				continue;
			tt[n] = nod;
			
			fmin = inf;
			for(nod = n; nod != 1; nod = tt[nod]){
				f[ tt[nod] ][nod] += fmin;
				f[nod][ tt[nod] ] -= fmin;
			}
			
			flow += fmin;
		}
		
	//out << flow;
	printf("%d", flow);
	
	return 0;
	}