Pagini recente » Cod sursa (job #1186820) | Cod sursa (job #281217) | Cod sursa (job #3031301) | Cod sursa (job #1784273) | Cod sursa (job #1747026)
#include <iostream>
#include <unordered_map>
#include <queue>
#include <fstream>
using namespace std;
int adia[1010][1010]; /// first is adiacent vertex, second is flow
int s, t, f, n;
int tata[100010], flux[100001];
bool get_new_blocking_flow(); /// false if there are no more new blocking flows possible
int mnm(int x, int y);
int main()
{
ifstream in("maxflow.in");
ofstream out("maxflow.out");
int m;
in >> n >> m/* >> s >> t*/;
s = 1;
t = n;
int fl, a, b;
while (m--) {
in >> a >> b >> fl; /// first vertex / second vertex / flow
adia[a][b] += fl;
}
while (get_new_blocking_flow()) { ; }
out << f << '\n';
return 0;
}
bool get_new_blocking_flow()
{
for (int i = 0; i <= n; i++)
tata[i] = 0, flux[i] = 0;
flux[s] = 1000000000;
queue <int> q;
q.push(s);
while (!q.empty()) {
int a = q.front();
q.pop();
if (a == t) {
f += flux[t];
while (a != s) {
adia[a][tata[a]] += flux[t];
adia[tata[a]][a] -= flux[t];
a = tata[a];
}
break;
}
for (int i = 1; i <= n; i++) {
if (flux[i] == 0 && adia[a][i] != 0 && i != a) {
flux[i] = mnm(adia[a][i], flux[a]);
tata[i] = a;
q.push(i);
}
}
}
return (bool)(flux[t]);
}
int mnm(int x, int y)
{
if (x < y)
return x;
return y;
}