Pagini recente » Cod sursa (job #2267658) | Cod sursa (job #823849) | Cod sursa (job #944909) | Cod sursa (job #907484) | Cod sursa (job #3196375)
#include <iostream>
#include <fstream>
#include <queue>
#include <cstring>
#include <utility>
#include <vector>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
vector<int>ls[10001],ls1[10001];
int c[10001][10001], f[10001][10001];
int tata[10001],viz[10001];
int bfs_lant(int s, int t) {
memset(tata, 0, sizeof tata);
tata[s] = -1;
queue<pair<int, int>>q;
q.push({ s,9999999 });
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] - f[x][y]) > 0) {
tata[y] = x;
int flux_nou = min(flux, c[x][y] - f[x][y]);
if (y == t) {
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;
int nod = t;
total += flux_nou;
while (nod != s) {
f[tata[nod]][nod] += flux_nou;
f[nod][tata[nod]] -= flux_nou;
nod = tata[nod];
}
}
return total;
}
int bfs(int s,int t,int k) {
queue<int>q;
viz[s] = 1;
q.push(s);
while (!q.empty()) {
int x = q.front();
q.pop();
for (auto y : ls1[x]) {
if (viz[y] == 0 && c[x][y] >= k)
{
viz[y] = 1;
q.push(y);
}
}
}
return viz[t];
}
int main() {
int n, m, capMax = 0;
in >> n >> m;
while (m--) {
int a, b, capacitate;
in >> a >> b >> capacitate;
ls[a].push_back(b);
ls[b].push_back(a);
c[a][b] = capacitate;
c[b][a] = capacitate;
f[a][b] = 0;
f[b][a] = capacitate;
}
int s = 1, t = n, k;
int fluxMax = edmondsKarp(s, t);
out << fluxMax;
}