Cod sursa(job #2284887)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 17 noiembrie 2018 18:46:26
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>
using namespace std;

/// by iterating all neighbours of the sink

const int DIM_MAX = 1010;

namespace Flow {
	
	int tata[DIM_MAX];
	int c[DIM_MAX][DIM_MAX];
	vector <int> adia[DIM_MAX];
	int s, d;

	bool bfs() {
		memset(tata, 0, sizeof tata);
		queue <int> q;
		q.push(s);
		tata[s] = -1;
		while (!q.empty()) {
			int nod = q.front();
			q.pop();
			for (auto i : adia[nod]) {
				if (c[nod][i] && !tata[i]) {
					tata[i] = nod;
					q.push(i);
				}
			}
		}
		return tata[d];
	}

	int augment() {
		int add = 1e9;
		for (int i = d; i != s; i = tata[i])
			add = min(add, c[tata[i]][i]);
		for (int i = d; i != s; i = tata[i])
			c[tata[i]][i] -= add, c[i][tata[i]] += add;
		assert(add != 0);
		return add;
	}

	int max_flow(int _s, int _d, int n) {
		s = _s, d = _d;
		int flow = 0;
		
		while (bfs()) {
			for (int i = 1; i <= n; i++) {
				if (c[i][d]) {
					assert(i != d);
					tata[d] = i;
					flow += augment();
				}
			}
		}

		return flow;
	}
}



int main()

{

	ifstream in("maxflow.in");

	ofstream out("maxflow.out");

	int n, m, a, b, c;

	in >> n >> m;

	while (m--) {

		in >> a >> b >> c;
		Flow::c[a][b] += c;
		Flow::adia[a].push_back(b);
		Flow::adia[b].push_back(a);

	}



	out << Flow::max_flow(1, n, n) << '\n';



	return 0;

}