Cod sursa(job #1769718)

Utilizator UPB_CodeJunkiesUPB NAIDEN NICOLICIOIU COTET UPB_CodeJunkies Data 3 octombrie 2016 00:12:10
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

const int NMAX = 1005;
const int INF = 0x3f3f3f3f;

int n, m;
int rez[NMAX][NMAX];
vector<int> g[NMAX];
int prev1[NMAX];
int totalFlow;

void read() {

	fin >> n >> m;

	for(int i = 1; i <= m ; ++i) {
		int x, y, c;
		fin >> x >> y >> c;
		g[x].push_back(y);
		g[y].push_back(x);
		rez[x][y] = c;
		rez[y][x] = 0;
	}
}

bool bfs(int s, int d) {

	int viz[NMAX]; 
	memset(viz, 0, sizeof(viz));
	
	queue<int> q;
	viz[s] = 1;
	q.push(s);

	while(q.empty() == false) {

		int node = q.front();

		for(int x : g[node])
			if(viz[x] == 0 && rez[node][x] > 0) {

				viz[x] = 1;
				q.push(x);
				prev1[x] = node;
			}

		q.pop();
	}

	return viz[d];
}

void solve() {

	int s = 1;
	int d = n;

	while( bfs(1, n) ) {

		prev1[s] = s;


		for(int node = 1; node <= n ; ++node) {

			if(rez[node][d] == 0) continue;

			int maxFlow = INF;
			prev1[d] = node;

			for(int i = d; i != prev1[i]; i = prev1[i]) 
				maxFlow = min(maxFlow, rez[prev1[i]][i]);

			totalFlow += maxFlow;

			for(int i = d; i != prev1[i] ; i = prev1[i]) {

				rez[prev1[i]][i] -= maxFlow;
				rez[i][prev1[i]] += maxFlow;
			}
		}
	}

	fout << totalFlow << '\n';

}

int main() {

	read(); solve();
	return 0;
}