Cod sursa(job #2421013)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 13 mai 2019 20:38:54
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>
using namespace std;

const int DIM = 1005;

vector<int> edg[DIM];
int cap[DIM][DIM], flo[DIM][DIM], rem[DIM], dis[DIM], que[DIM];

bool bfs(int src, int dst) {
	memset(dis, 0, sizeof dis);
	memset(rem, 0, sizeof rem);
	dis[src] = 1; que[1] = src;
	for (int st = 1, en = 1; st <= en; ++st) {
		int x = que[st];
		for (int y : edg[x]) {
			if (flo[x][y] < cap[x][y] and !dis[y]) {
				dis[y] = dis[x] + 1; que[++en] = y;
				if (y == dst) {
					return true; } } } }
	return false; }

int dfs(int x, int dst, int f) {
	if (x == dst) {
		return f; }
	else {
		for (; rem[x] < edg[x].size(); ++rem[x]) {
			int y = edg[x][rem[x]];
			if (dis[y] == dis[x] + 1 and flo[x][y] < cap[x][y]) {
				int ff = dfs(y, dst, min(f, cap[x][y] - flo[x][y]));
				if (ff) {
					flo[x][y] += ff; flo[y][x] -= ff;
					return ff; } } } 
		return 0; } }

int main(void) {
	freopen("maxflow.in", "r", stdin);
	freopen("maxflow.out", "w", stdout);
	int n, m; cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		int x, y, c; cin >> x >> y >> c;
		edg[x].push_back(y);
		edg[y].push_back(x);
		cap[x][y] = c; }
	int ans = 0;
	while (bfs(1, n)) {
		int aux;
		while (aux = dfs(1, n, 110000)) {
			ans += aux; } }
	cout << ans;
	return 0; }