library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub yuruhi/library

:heavy_check_mark: test/FordFulkerson.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_6_A"
#include "./../Graph/FordFulkerson.cpp"
#include <iostream>
using namespace std;

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	FordFulkerson g(n);
	for (int i = 0; i < m; ++i) {
		int u, v;
		FLOW d;
		cin >> u >> v >> d;
		g.add_edge(u, v, d);
	}
	cout << g.solve(0, n - 1) << '\n';
}
#line 1 "test/FordFulkerson.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_6_A"
#line 2 "Graph/FlowTemplate.cpp"
#include <vector>
#include <iostream>
#include <limits>

using FLOW = long long;
constexpr FLOW INF_FLOW = std::numeric_limits<FLOW>::max();
struct EdgeF {
	int to, rev;
	FLOW cap;
	EdgeF() : to(-1), rev(-1), cap(-1) {}
	EdgeF(int t, int r, FLOW c) : to(t), rev(r), cap(c) {}
	friend std::ostream& operator<<(std::ostream& os, const EdgeF& e) {
		return os << "->" << e.to << "(" << e.cap << ")";
	}
};
using GraphF = std::vector<std::vector<EdgeF>>;
#line 4 "Graph/FordFulkerson.cpp"

class FordFulkerson {
	int n;
	GraphF graph;
	std::vector<bool> used;
	FLOW dfs(int v, int t, FLOW f) {
		if (v == t) return f;
		used[v] = true;
		for (auto& e : graph[v]) {
			if (!used[e.to] && e.cap > 0) {
				FLOW d = dfs(e.to, t, std::min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					graph[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}

public:
	FordFulkerson(std::size_t n) :  graph(n), used(n) {}
	const GraphF& get_G() {
		return graph;
	}
	void add_edge(int from, int to, FLOW cap) {
		graph[from].emplace_back(to, graph[to].size(), cap);
		graph[to].emplace_back(from, graph[from].size() - 1, 0);
	}
	FLOW solve(int s, int t) {
		FLOW result = 0;
		while (true) {
			std::fill(used.begin(), used.end(), false);
			FLOW f = dfs(s, t, INF_FLOW);
			if (!f) return result;
			result += f;
		}
	}
};
#line 4 "test/FordFulkerson.test.cpp"
using namespace std;

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	FordFulkerson g(n);
	for (int i = 0; i < m; ++i) {
		int u, v;
		FLOW d;
		cin >> u >> v >> d;
		g.add_edge(u, v, d);
	}
	cout << g.solve(0, n - 1) << '\n';
}
Back to top page