Cod sursa(job #1955528)

Utilizator bt.panteaPantea Beniamin bt.pantea Data 6 aprilie 2017 01:31:22
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2 kb
// Flux Maxim.cpp : Defines the entry point for the console application.
//

#include "iostream"
#include "fstream"
#include "vector"
#include <cstring>
#define NMAX 1005
#define INF 1000000000
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int flux[NMAX][NMAX], n, m;
int Father[NMAX], frezmin, MinFather[NMAX];
int bf[NMAX];
vector < int > a[NMAX];

inline int min(const int & a, const int & b) {
	if (a < b) return a;
	return b;
}

void UpdateFluxRezidual(const int & nod, const int & value, const int & source) {
	if (nod == source) return;
	flux[Father[nod]][nod] -= value;
	UpdateFluxRezidual(Father[nod], value, source);
}

inline bool BF(const int & source, const int & destination) {
	memset(Father, 0, sizeof(Father));
	bf[0] = source;
	int nr = 1;
	Father[source] = source;
	int _index = 0;
	while (_index < nr) {
		int nod = bf[_index];
		_index++;
		if (nod == destination) continue;
		for (int i = 0; i < a[nod].size(); i++) {
			int next = a[nod][i];
			if (flux[nod][next] > 0 && Father[next] == 0) {
				Father[next] = nod;
				bf[nr] = next;
				nr++;
			}
		}
	}
	if (Father[destination]) return true;
	return false;
}

int maxflow()
{
	int source = 1, destination = n;
	int flow = 0;
	for (; BF(source, destination);) {
		for (int i = 0; i < a[destination].size(); i++) {
			int nod = a[destination][i];
			if (flux[nod][destination] <= 0 || Father[nod] == 0) continue;
			Father[destination] = nod;
			int flowmin = INF;
			for (nod = destination; nod != source; nod = Father[nod])
				flowmin = min(flowmin, flux[Father[nod]][nod]);
			for (nod = destination; nod != source; nod = Father[nod]) {
				flux[Father[nod]][nod] -= flowmin;
				flux[nod][Father[nod]] += flowmin;
			}
			flow += flowmin;
		}
	}
	return flow;
}

int main()
{
	f >> n >> m;
	for (int i = 0; i < m; i++) {
		int x, y, c;
		f >> x >> y >> c;
		a[x].push_back(y);
		a[y].push_back(x);
		flux[x][y] += c;
	}
	g << maxflow();
    return 0;
}