Cod sursa(job #1955487)

Utilizator bt.panteaPantea Beniamin bt.pantea Data 6 aprilie 2017 00:23:24
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 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 capacitate[NMAX][NMAX], flux[NMAX][NMAX], frezidual[NMAX][NMAX], n, m;
int Father[NMAX], frezmin, MinFather[NMAX];
vector < int > a[NMAX];

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

bool DF(const int & nod, const int & father, const int & destination) {
	Father[nod] = father;
	if (nod == destination) return true;
	for (int i = 0; i < a[nod].size(); i++) {
		int next = a[nod][i];
		if (frezidual[nod][next] > 0 && Father[next] == 0) {
			MinFather[next] = min(MinFather[nod], frezidual[nod][next]);
			if (DF(next, nod, destination)) return true;
		}
	}
	return false;
}

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

int maxflow()
{
	int source = 1, destination = n;
	int flow = 0;
		vector < int > dfs;
	bool sw;
	do {
		memset(Father, 0, sizeof(Father));
		MinFather[source] = INF;
		sw = DF(source, source, destination);
		if (sw) {
			flow += MinFather[destination];
			UpdateFluxRezidual(destination, MinFather[destination], source);
		}
	} while (sw);
	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);
		capacitate[x][y] = c;
		frezidual[x][y] = c;
		frezidual[y][x] = -c;
	}
	g << maxflow();
    return 0;
}