Cod sursa(job #294479)

Utilizator whiskeyOzzy Osbourne whiskey Data 2 aprilie 2009 16:08:03
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

int n, m, maxflow = 0;
int a[1001][1001];
int t[1001];
queue<int> c;

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

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

bool bfs()
{
	int i, nod;
	memset(t, 0, sizeof(t));
	while (!c.empty())
	{
		c.pop();
	}
	c.push(1);
	while (!c.empty())
	{
		nod = c.front();
		c.pop();
		for (i = 1; i <= n; ++i)
		{
			if (a[nod][i] && !t[i])
			{
				t[i] = nod;
				c.push(i);
				if (i == n)
				{
					return 1;
				}
			}
		}
	}
	return 0;
}

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

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

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