Cod sursa(job #289264)

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

int n, maxflow = 0, flow;
int a[1001][1001];
int t[1001];

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

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

void bfs()
{
	flow = 1000000;
	int i, nod;
	list<int> coada;
	coada.push_back(1);
	while (!coada.empty())
	{
		nod = *coada.begin();
		for (i = 1; i <= n; ++i)
		{
			if (a[nod][i] && !t[i])
			{
				t[i] = nod;
				coada.push_back(i);
				if (a[nod][i] < flow)
				{
					flow = a[nod][i];
				}
			}
		}
		coada.pop_front();
	}
}

void solve()
{
	int i;
	flow = 1000000;
	while (flow)
	{
/*		for (i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= n; ++j)
			{
				printf("%d ", a[i][j]);
			}
			printf("\n");
		}
		printf("\n");
*/		bfs();
		if (!t[n])
		{
			break;
		}
		for (i = n; i; i = t[i])
		{
			a[t[i]][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);
}