Cod sursa(job #1366105)

Utilizator cristian.enciuCristian Enciu cristian.enciu Data 28 februarie 2015 19:44:57
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
#include<bitset>

using namespace std;


int cap[1010][1010];
int father[1010];
bitset<1010> vis;

vector<int> nodes[1010];
queue<int> q;
int n, m, le_max_flow;

void bfs()
{
	vis.set();

	q.push(1);
	vis[1] = 0;
	while(!q.empty()) {
		int ind = q.front();
		q.pop();

		for(int next : nodes[ind]) {
			if(vis[next] && (cap[ind][next] > 0)) {
				q.push(next);
				vis[next] = 0;
				father[next] = ind;
			}
		}
	}
}

int main()
{
	freopen("maxflow.in", "r", stdin);
	freopen("maxflow.out", "w", stdout);

	scanf("%d%d", &n, &m);

	for(int i = 0 ; i < m ; ++i) {
		int x, y, val;
		scanf("%d%d%d", &x, &y, &val);

		nodes[x].push_back(y);
		nodes[y].push_back(x);
		cap[x][y] = val;
	}

	for(bfs(); !vis[n] ; bfs()) {
		for(int ind : nodes[n]) {

			if(vis[ind] || cap[ind][n] <= 0) continue;

			int max_flow = cap[ind][n];
			for(int i = ind ; i != 1 ; i = father[i])
				max_flow = max_flow <= cap[father[i]][i] ? max_flow : cap[father[i]][i];

			cap[ind][n] -= max_flow;
			cap[n][ind] += max_flow;
			for(int i = ind ; i != 1 ; i = father[i]) {
				cap[father[i]][i] -= max_flow;
				cap[i][father[i]] += max_flow;
			}

			le_max_flow += max_flow;
		}
	}

	printf("%d\n", le_max_flow);

	return 0;
}