Cod sursa(job #2710130)

Utilizator bubblegumixUdrea Robert bubblegumix Data 21 februarie 2021 21:32:33
Problema Flux maxim Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.62 kb
#include<iostream>
#include<fstream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string.h>
#include<string>
#define N 1005
#define M 5005
using namespace std;
typedef long long ll;

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

int flow_g[N][N],res_g[N][N],parent[N];
int n, m;
int maxflow;

void read()
{
	f >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		int x, y;
		f >> x >> y;
		f >> flow_g[x][y];
	}
}
void printq(queue<int> q)
{
	while (!q.empty())
	{
		cout << q.front() << " ";
		q.pop();

	}
	cout << endl;
}
bool bfs(int g[N][N], int source, int sink)
{
	bool isUsed[N];
	memset(isUsed, false, sizeof(isUsed));
	queue<int> q;
	q.push(source);
	isUsed[source] = true;
	parent[source] = -1;
	while (!q.empty())
	{
		//printq(q);
		int u = q.front();
		q.pop();
	
		for(int v=1;v<=n;v++)
			if (isUsed[v]==false && g[u][v] > 0)
			{
				
				q.push(v);
				parent[v] = u;
				isUsed[v] = true;
			}
	}
	
	return isUsed[sink];
	
}
void ed_karp()
{
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			res_g[i][j] = flow_g[i][j];

	


	while (bfs(res_g, 1, n))
	{
		int bottleneck = 200000;
		for (int i = n; i != 1; i = parent[i])
		{
			
			int j = parent[i];
			if (res_g[j][i] < bottleneck)
				bottleneck = res_g[j][i];
		}
		for (int i = n; i != 1; i = parent[i])
		{
			
			int j = parent[i];
			res_g[j][i] -= bottleneck;
			res_g[i][j] += bottleneck;

		}
		maxflow += bottleneck;
		

	
	}
	g << maxflow;
}

int main()
{
	ios_base::sync_with_stdio(false);
	f.tie(0);
	read();
	ed_karp();
	return 0;
}