Pagini recente » Cod sursa (job #2386575) | Cod sursa (job #2262628) | Cod sursa (job #2927264) | Cod sursa (job #2872392) | Cod sursa (job #2949866)
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
#define mx 1001
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int n, m, x, y, capacitate;
int rgraf[mx][mx];
int tata[mx];
bool bfs(int s, int d) {
queue<int> q;
vector<bool> vizitat(n + 1, false);
vizitat[s] = true;
tata[s] = -1;
q.push(s);
while (!q.empty()) {
int front = q.front();
q.pop();
for (int i = 1; i < n + 1; ++i) {
if (vizitat[i] == false && rgraf[front][i] > 0) {
vizitat[i] = true;
q.push(i);
tata[i] = front;
}
}
}
//daca se poate ajunge la destinatie
return vizitat[d];
}
int main() {
f >> n >> m;
for (int i = 0; i < m; ++i) {
f >> x >> y >> capacitate;
rgraf[x][y] = capacitate;
rgraf[y][x] = capacitate;
}
int fluxTotal = 0;
while (bfs(1,n)) {
int flux = INT_MAX;
int u = n;
while (u != 1) {
int x = rgraf[tata[u]][u];
flux = min(flux, x);
u = tata[u];
}
u = n;
while (u != 1) {
rgraf[tata[u]][u] -=
flux;
// rgraf[tata[u]][u] +=
// flux;
u = tata[u];
}
fluxTotal += flux;
}
g<<fluxTotal;
return 0;
}