Cod sursa(job #289290)

Utilizator whiskeyOzzy Osbourne whiskey Data 26 martie 2009 17:37:55
Problema Flux maxim Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <stdio.h>
#include <queue>
using namespace std;

int n, maxflow = 0, flow;
int a[1001][1001];
int t[1001];
queue<int> coada;

void read();
void solve();
void write();
void bfs();

int main()
{
	read();
	solve();
	write();
	return 0;
}

void bfs()
{
	flow = 1000000;
	int i, nod;
	while (!coada.empty())
	{
		coada.pop();
	}
	coada.push(1);
	while (!coada.empty())
	{
		nod = coada.front();
		for (i = 1; i <= n; ++i)
		{
			if (a[nod][i] && !t[i])
			{
				t[i] = nod;
				coada.push(i);
				if (a[nod][i] < flow)
				{
					flow = a[nod][i];
				}
				if (i == n)
				{
					while (!coada.empty())
					{
						coada.pop();
					}
					return;
				}
			}
		}
		coada.pop();
	}
}

void solve()
{
	int i;
	flow = 1000000;
	while (flow)
	{
		bfs();
		if (!t[n])
		{
			break;
		}
		for (i = n; i; i = t[i])
		{
			a[t[i]][i] -= flow;
		//	a[i][t[i]] += flow;
		}
		maxflow += flow;
		for (i = 1; i <= n; ++i)
		{
			t[i] = 0;
		}
	}
}

void write()
{
	FILE *fout = fopen("maxflow.out", "w");
	fprintf(fout, "%d\n", maxflow);
	fclose(fout);
}

void read()
{
	int m, x, y, z;
	FILE *fin = fopen("maxflow.in", "r");
	fscanf(fin, "%d%d", &n, &m);
	for (; m; --m)
	{
		fscanf(fin, "%d%d%d", &x, &y, &z);
		a[x][y] = z;
	}
	fclose(fin);
}