Cod sursa(job #2083172)

Utilizator Juve45UAIC Alexandru Ionita Juve45 Data 7 decembrie 2017 09:34:33
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <bits/stdc++.h>

#define dbg(x) cerr<<#x": "<<x<<"\n"
#define dbg_p(x) cerr<<#x": "<<x.first<<","<<x.second<<"\n"
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

#define DMAX 10100
#define NMAX 1
#define MMAX 1

using namespace std;

int n, k, x, m, src, dest, use[DMAX], from, to, cost, flow;
string s;

struct edge{
	int x, y, c, f;

	edge(int _x, int _y, int _c, int _f)
	{
		x = _x;
		y = _y;
		c = _c;
		f = _f;
	}
};
vector <int> v[DMAX];
vector <edge> e;

int dfs(int node, int flow)
{
	if(!flow || node == dest)
		return flow;
	for(auto i : v[node])
	{
		if(use[e[i].y])
			continue;
		if(e[i].f < e[i].c)
		{
			int u = e[i].y;
			use[u] = 1;
			int recv = dfs(u, min(flow, e[i].c - e[i].f));
			use[u] = 0;
			e[i].f += recv;
			e[i ^ 1].f -= recv;
			if(recv > 0)
				return recv;
		}
	}
	return 0;
}

int main()
{
	ios_base::sync_with_stdio(false);

	freopen("maxflow.in", "r", stdin);
	freopen("maxflow.out", "w", stdout);
	//ios_base::sync_with_stdio(false);
	cin >> n >> m;
	for(int i = 1; i <= m; i++)
	{
		cin >> from >> to >> cost;
		v[from].push_back(e.size());
		e.push_back(edge(from, to, cost, 0));
		v[to].push_back(e.size());
		e.push_back(edge(to, from, 0, 0));
	}
	//cin >> src >> dest;
	src = 1;
	dest = n;
	use[src] = 1;
	int fl = dfs(src, 100000);
	while(fl > 0){
		memset(use, 0, sizeof use);
		flow += fl;
		use[1] = 1;
		fl = dfs(src, 100000);
		}
	cout << flow << '\n';
}