Cod sursa(job #2424195)

Utilizator raduzxstefanescu radu raduzx Data 22 mai 2019 19:27:21
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>

using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
#define nmax 1003
int v[nmax][nmax];
int flowGraph[nmax][nmax];
int rezid[nmax][nmax];
int n, m;
vector<int> vecini[nmax];
bool used[nmax];
int father[nmax];
int bfs() {
	queue<int> q;
	q.push(1);
	for (int i = 0; i <= n; i++)
	{
		used[i] = 0; father[i] = 0;
	}
	used[1] = 1;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (auto elem : vecini[u]) {
			if (used[elem] == 0 && v[u][elem] != flowGraph[u][elem]) {
				q.push(elem);
				used[elem] = 1;
				father[elem] = u;
			}
		}
	}
	return used[n];
}


int main() {
	int x, y, c;
	f >> n >> m;
	
	for (int i = 1; i <= m; i++)
	{
		f >> x >> y >> c;
		vecini[x].push_back(y);
		vecini[y].push_back(x);
		v[x][y] += c;
	}
	int max_flow = 0;
	int count = 0;
	while (bfs() == 1) {
		int init,flow_act=2000000000;
		for (init = n; init != 1; init = father[init]) {
			int u = father[init];
			flow_act = min(flow_act, v[u][init]-flowGraph[u][init]);
		}
		max_flow += flow_act;
		for (init = n; init != 1; init = father[init]) {
			int u = father[init];
			flowGraph[u][init] += flow_act;
			flowGraph[init][u] -= flow_act;
		}
		count++;
	}
	g << max_flow << '\n';
	//g << count;
	return 0;
}