Cod sursa(job #2284864)

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

struct Edge {

	int from, to, cap;

};


namespace Flow {

	vector <Edge> edges;

	vector <int> adia[1010];

	bool viz[1010];



	void add_edge(int from, int to, int cap) {

		adia[from].push_back(edges.size());

		edges.push_back({ from, to, cap });

		adia[to].push_back(edges.size());

		edges.push_back({ to, from, 0 });

	}



	bool go(int s, int t, int c) {

		if (viz[s])

			return false;

		viz[s] = 1;

		if (s == t)

			return true;

		for (auto i : adia[s]) {

			if (edges[i].cap >= c && go(edges[i].to, t, c)) {

				edges[i].cap -= c;

				edges[i ^ 1].cap += c;

				return true;

			}

		}

		return false;

	}

	int max_flow(int s, int t) {

		int f = 0, c = 1e9;

		while (c) {

			memset(viz, 0, sizeof viz);

			if (go(s, t, c))

				f += c;

			else

				c /= 2;

		}

		return f;

	}

}

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::add_edge(a, b, c);

	}



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



	return 0;

}