Cod sursa(job #812108)

Utilizator ahmed.abdraboahmed.abdrabo ahmed.abdrabo Data 13 noiembrie 2012 15:04:58
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>

using namespace std;

inline int next_int() {
	int d;
	scanf("%d", &d);
	return d;
}

const int V = 1 << 10;
const int E = 1 << 14;

int ec, eb[V], en[E], et[E];

int n, m;
int cap[V][V], Q[V], seen[V], from[V];

bool dfs(int s, int d, int flow) {
	if (s == d) {
		return true;
	}
	if (seen[s]) {
		return false;
	}
	seen[s] = true;
	for (int e = eb[s]; e != -1; e = en[e]) {
		int v = et[e];
		if (seen[v] == false && cap[s][v] >= flow && dfs(v, d, flow)) {
			cap[s][v] -= flow;
			cap[v][s] += flow;
			return true;
		}
	}
	return false;
}

int main() {
	freopen("maxflow.in", "r", stdin);
	freopen("maxflow.out", "w", stdout);
	memset(eb, -1, sizeof eb);
	n = next_int();
	m = next_int();
	for (int i = 0; i < m; i++) {
		int u = next_int();
		int v = next_int();
		et[ec] = v;
		en[ec] = eb[u];
		eb[u] = ec++;
		et[ec] = u;
		en[ec] = eb[v];
		eb[v] = ec++;
		cap[u][v] += next_int();
	}
	int max_flow = 0;
	for (int flow = 1 << 20; flow; flow >>= 1) {
		memset(seen, 0, sizeof seen);
		while (dfs(1, n, flow)) {
			max_flow += flow;
			memset(seen, 0, sizeof seen);
		}
	}
	printf("%d\n", max_flow);
	return 0;
}