Cod sursa(job #1601819)

Utilizator qwertyuiTudor-Stefan Berbinschi qwertyui Data 16 februarie 2016 11:39:57
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <tuple>
#include <queue>

using namespace std;

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

#define MAXN 1050
#define MAXM 5050
#define MAXC 110050
#define INFTY 99000050


vector < vector <int> > edge;
int N, M;

vector <int> used;
int flow[MAXN][MAXN], parent[MAXN], capacity[MAXN][MAXN];

int BFS ()
{
	queue <int> NodeQ;
	fill(used.begin(),used.end(),0);
	NodeQ.push(1);
	used[1] = 1;

    while (!NodeQ.empty())
	{
		int node = NodeQ.front();
		NodeQ.pop();
		if (node == N)
			continue;
		for (int i = 0; i < edge[node].size(); ++i)
		{
			int destNode = edge[node][i];
			if (capacity[node][destNode] == flow[node][destNode] || used[destNode])
				continue;
            used[destNode] = 1;
            NodeQ.push(destNode);
            parent[destNode] = node;
		}
	}

	return used[N];
}

int main()
{
    fin >>N >>M;

    edge.resize(N+1);
    used.resize(N+1);

    for (int i = 1; i <= M; ++i)
	{
		int x,y,c;
		fin >>x >>y >>c;
		capacity[x][y] += c;
		edge[x].push_back(y);
		edge[y].push_back(x);
	}

	int _flow;

	for (_flow = 0; BFS(); )
		for (int i = 0; i < edge[N].size(); ++i)
		{
			int curr_node = edge[N][i];
			if (flow[curr_node][N] == capacity[curr_node][N] || !used[curr_node])
				continue;

			parent[N] = curr_node;
			int flow_min = INFTY;

			for (curr_node = N; curr_node != 1; curr_node = parent[curr_node])
				flow_min = (flow_min < (capacity[parent[curr_node]][curr_node] - flow[parent[curr_node]][curr_node])) ? (flow_min) : (capacity[parent[curr_node]][curr_node] - flow[parent[curr_node]][curr_node]);

			if (flow_min == 0)
				continue;

			for (curr_node = N; curr_node != 1; curr_node = parent[curr_node])
			{
				flow[parent[curr_node]][curr_node] += flow_min;
				flow[curr_node][parent[curr_node]] -= flow_min;
          	}

          	_flow += flow_min;
		}

	fout <<_flow <<'\n';

    return 0;
}