Cod sursa(job #1746612)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 23 august 2016 17:07:11
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <iostream>
#include <map>
#include <queue>
#include <fstream>
using namespace std;

map <int, int> adia[100010]; /// first is adiacent vertex, second is flow

int s, t, f, n;
int tata[100010], flux[100001];
bool get_new_blocking_flow(); /// false if there are no more new blocking flows possible

int mnm(int x, int y);

int main()
{
	ifstream in("maxflow.in");
	ofstream out("maxflow.out");
	int m;
	in >> n >> m/* >> s >> t*/;
	s = 1;
	t = n;
	int fl, a, b;
	while (m--) {
		in >> a >> b >> fl; /// first vertex / second vertex / flow
		adia[a][b] += fl;
	}
	while (get_new_blocking_flow()) { ; }
	out << f << '\n';
	return 0;
}

bool get_new_blocking_flow()
{
	for (int i = 0; i <= n; i++)
		tata[i] = 0, flux[i] = 0;

	flux[s] = 1000000000;
	queue <int> q;
	q.push(s);

	while (!q.empty()) {
		int n = q.front();
		q.pop();
		if (n == t) {
			f += flux[t];
			while (n != s) {
				adia[n][tata[n]] += flux[t];
				adia[tata[n]][n] -= flux[t];
				if (adia[tata[n]][n] == 0)
					adia[tata[n]].erase(n);
				n = tata[n];
			}
			break;
		}
		for (auto i : adia[n]) {
			if (flux[i.first] == 0) {
				flux[i.first] = mnm(i.second, flux[n]);
				tata[i.first] = n;
				q.push(i.first);
			}
		}
	}
	return (bool)(flux[t]);
}

int mnm(int x, int y)
{
	if (x < y)
		return x;
	return y;
}