Cod sursa(job #2907757)

Utilizator CyborgSquirrelJardan Andrei CyborgSquirrel Data 31 mai 2022 16:21:32
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const int V = 1041;
const int E = 50041;

int v, e;
int cap[V][V], res[V][V];
vector<int> adi[V];
int dp[V], pr[V];

int gen = 0;
int vi[V];

int s, t;

int main(int argc, char **argv) {
	ifstream fin("maxflow.in");
	ofstream fout("maxflow.out");
	
	fin >> v >> e;
	s = 1; t = v;
	for (int i = 0; i < e; ++i) {
		int a, b, c;
		fin >> a >> b >> c;
		cap[a][b] = c;
		adi[a].push_back(b);
		adi[b].push_back(a);
		if (a == s) {
			dp[a] += c;
		}
	}
	
	auto find_path = []() {
		gen++;
		queue<int> q;
		vi[s] = gen;
		q.push(s);
		while (!q.empty()) {
			int a = q.front(); q.pop();
			for (auto b : adi[a]) {
				if (vi[b] != gen) {
					dp[b] = min(dp[a], cap[a][b] - res[a][b]);
					if (dp[b] > 0) {
						vi[b] = gen;
						pr[b] = a;
						if (b == t) {
							return true;
						}
						q.push(b);
					}
				}
			}
		}
		return false;
	};
	
	int max_flow = 0;
	while (find_path()) {
		max_flow += dp[t];
		int a = t;
		while (a != s) {
			res[pr[a]][a] += dp[t];
			res[a][pr[a]] -= dp[t];
			a = pr[a];
		}
	}
	
	fout << max_flow;
	
	return 0;
}