Cod sursa(job #3196422)

Utilizator DrioanDragos Ioan Drioan Data 23 ianuarie 2024 20:42:28
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");

vector<int>ls[1001], ls1[1001];
int c[1001][1001], f[1001][1001];
int fluxin[1001], fluxout[1001];
int viz[1001], tata[1001],n,m;
vector<pair<int, int>>muchii, arce_directe;

int bfs_lant(int s, int t) {
	queue<pair<int, int>>q;
	memset(tata, 0, sizeof tata);
	q.push({ 1,9999999 });
	tata[1] = -1;
	while (!q.empty()) {
		int x = q.front().first;
		int flux = q.front().second;
		for (auto y : ls[x]) {
			if (tata[y] == 0 && (c[x][y]) > 0) {
				tata[y] = x;
				int flux_nou = min(flux, c[x][y]);
				if (y == n) {
					return flux_nou;
				}
				q.push({ y,flux_nou });
			}
		}
		q.pop();
	}
	return -1;
}



int edmondsKarp(int s, int t) {
	int total = 0;
	while (true) {
		int flux_nou = bfs_lant(s, t);
		if (flux_nou == -1)
			break;
		total += flux_nou;
		int nod = n;
		while (nod != 1) {

			c[nod][tata[nod]] += flux_nou;
			c[tata[nod]][nod] -= flux_nou;
			nod = tata[nod];
		}
	}
	return total;
}

int main() {
	int  s, t;
	in >> n;
	
	in >> m;
	s = 1;
	t = n;
	while (m--) {
		int a, b, capacitate, flux = 0;
		in >> a >> b >> capacitate;
		ls[a].push_back(b);
		ls[b].push_back(a);
		c[a][b] = capacitate;
		
	}


	out << edmondsKarp(1, n) ;

}