Cod sursa(job #1133272)

Utilizator antonioteoZait Teodor Antonio antonioteo Data 4 martie 2014 18:05:22
Problema Flux maxim Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <algorithm>
#include <fstream>
#include <cstring>
#include <queue>
using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

#define NMAX 1001
#define Node first
#define mp make_pair 
#define INF 0x3f3f3f3f
#define Capacity second
#define PII pair <int, int>

int i, j, N, M;
int A, B, n, c;
int x, minC;
int S, D;

bool used[NMAX];
int Father[NMAX];
queue <int> Q;

PII Network[NMAX][NMAX];
int pos[NMAX][NMAX];

int MinCapacity() {
	memset(used, 0, sizeof(used));
	memset(Father, 0, sizeof(Father));
	minC = INF;
	Q.push(S);
	used[S] = true;
	PII n;
	while (!Q.empty()) {
		x = Q.front();
		for (i = 1; i <= pos[x][0]; ++i) {
			n = Network[x][i];
			if (!used[n.Node] && n.Capacity){
				minC = min(minC, n.Capacity);
				Father[n.Node] = x;
				used[n.Node] = true;
				Q.push(n.Node);
			}
		}
		Q.pop();
	}
	if (used[D] == false || minC == INF) 
		return INF;
	x = D;
	while (Father[x]) {
		Network[Father[x]][pos[Father[x]][x]].Capacity -= minC;
		Network[x][pos[x][Father[x]]].Capacity += minC;
		x = Father[x];
	}
	return minC;
}

int cnt, MaxFlow;

int main() {
	fin >> N >> M;
	for (i = 1; i <= M; ++i) {
		fin >> A >> B >> c;
		Network[A][++pos[A][0]] = mp(B, c);
		pos[A][B] = pos[A][0];
	}
	S = 1; 
	D = N;
	while (1) {
		cnt = MinCapacity();
		if (cnt == INF)
			break;
		MaxFlow += cnt;
	}
	fout << MaxFlow << '\n';
	return 0;
}