Cod sursa(job #2427027)

Utilizator geo_furduifurdui geo geo_furdui Data 30 mai 2019 16:16:02
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include<iostream>
#include<fstream>
#include<vector>

using namespace std;

vector<int> g[10010];
int cost[1001][1001];
int flow[1001][1001];
int dad[1001], k;
int n, m, flux, viz[100010];

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

void read()
{
	in >> n >> m;
	int a, b, x;
	for (int i = 1; i <= m; i++)
	{
		in >> a >> b >> x;
		g[a].push_back(b);
		g[b].push_back(a);
		cost[a][b] = x;
	}
}
int bfs()
{
	k++;
	int coada[1010], pi = 1;
	coada[1] = 1;
	for (int ps = 1; ps <= pi; ps++)
	{
		int nod = coada[ps];
		for (int i = 0; i < g[nod].size(); i++)
		{
			int neigh = g[nod][i];
			if (cost[nod][neigh] - flow[nod][neigh] > 0 && viz[neigh]<k)
			{
				viz[neigh] = k;
				dad[neigh] = nod, coada[++pi] = neigh; if (neigh == n) return 1;
			}
		}
	}
	return 0;

}
void fulk()
{
	while (bfs() == 1)
	{
		int minim = 1000000000;
		for (int x = n; x != 1; x = dad[x])
		{
			if (cost[dad[x]][x] - flow[dad[x]][x] < minim) minim = cost[dad[x]][x] - flow[dad[x]][x];
		}
		flux += minim;
		for (int x = n; x != 1; x = dad[x])
		{
			flow[dad[x]][x] += minim;
			flow[x][dad[x]] -= minim;
		}
	}
}
int main()
{
	read();
	fulk();
	out << flux;
	return 0;
}