Pagini recente » Cod sursa (job #720254) | Cod sursa (job #2152095) | Cod sursa (job #402325) | Cod sursa (job #1938229) | Cod sursa (job #2386503)
#include <fstream>
#include <bitset>
#include <queue>
#include <vector>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int n, m, F[1001][1001], C[1001][1001];
vector<int> G[1001];
bitset<1001> viz;
int t[1001];
int bfs()
{
viz.reset();
queue<int> q;
q.push(1);
viz[1] = 1;
while (!q.empty())
{
int el = q.front();
q.pop();
if (el == n)
continue;
for (auto it : G[el])
{
if (C[el][it] != F[el][it] && !viz[it])
{
q.push(it);
t[it] = el;
viz[it] = 1;
}
}
}
return viz[n];
}
int main()
{
f >> n >> m;
int x, y, z;
for (; m; m--)
{
f >> x >> y >> z;
C[x][y] = z;
G[x].push_back(y);
G[y].push_back(x);
}
int flow = 0;
while (bfs())
{
for (auto it : G[n])
{
if (F[it][n] != C[it][n] && viz[it])
{
int fm = 111000;
t[n] = it;
for (int node = n; node != 1; node = t[node])
{
int nf = C[t[node]][node] - F[t[node]][node];
if (nf < fm)
fm = nf;
}
for (int node = n; node != 1; node = t[node])
{
F[t[node]][node] += fm;
F[node][t[node]] -= fm;
}
flow += fm;
}
}
}
g << flow;
return 0;
}