Cod sursa(job #2428513)

Utilizator raduzxstefanescu radu raduzx Data 5 iunie 2019 17:12:09
Problema Flux maxim Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");

#define nmax 1005
#define inf 2000000000
int n, m;
int mat[nmax][nmax];
int flowGraph[nmax][nmax];
int used[nmax];
int p[nmax];
void init() {
	for (int i = 1; i <= n; i++) {
		p[i] = used[i] = 0;
	}
}
void dfs(int rad) {
	used[rad] = 1;
	for (int i = 1; i <= n; i++) {
		if (used[i] == 0 and mat[rad][i] > flowGraph[rad][i]) {
			p[i] = rad;
			dfs(i);
		}
	}

}
int main() {
	f >> n >> m;
	int x, y, c;
	for (int i = 1; i <= m; i++) {
		f >> x >> y >> c;
		mat[x][y] += c;
	}
	int total = 0;
	init();
	dfs(1);
	while (used[n]) {
		int maxFlow = inf,cn=n;
		while (p[cn] != 0) {
			maxFlow = min(maxFlow, mat[p[cn]][cn] - flowGraph[p[cn]][cn]);
			cn = p[cn];

		}
		cn = n;
		while (p[cn] != 0) {
			flowGraph[p[cn]][cn] += maxFlow;
			flowGraph[cn][p[cn]] -= maxFlow;
			cn = p[cn];
		}
		total += maxFlow;
		init();
		dfs(1);
	}
	g << total;
	return 0;
}