Pagini recente » Cod sursa (job #2033995) | Cod sursa (job #1025662) | Cod sursa (job #2141722) | Cod sursa (job #2547051) | Cod sursa (job #3040685)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("maxflow.in");
ofstream out ("maxflow.out");
const int max_size = 1e3 + 1, INF = 1e9 + 1;
int cap[max_size][max_size], viz[max_size], t[max_size], n;
vector <int> mc[max_size];
bool bfs ()
{
for (int i = 1; i <= n; i++)
{
viz[i] = 0;
t[i] = 0;
}
viz[1] = 1;
queue <int> q;
q.push(1);
while (!q.empty())
{
int nod = q.front();
q.pop();
for (auto f : mc[nod])
{
if (!viz[f] && cap[nod][f] > 0)
{
viz[f] = 1;
t[f] = nod;
q.push(f);
if (f == n)
{
return true;
}
}
}
}
return false;
}
int main ()
{
int m;
in >> n >> m;
while (m--)
{
int x, y, c;
in >> x >> y >> c;
cap[x][y] += c;
mc[x].push_back(y);
mc[y].push_back(x);
}
int flux = 0;
while (bfs())
{
for (auto f : mc[n])
{
if (!viz[f] || cap[f][n] <= 0)
{
continue;
}
t[n] = f;
int nod = n, mn = INF;
while (nod != 1)
{
mn = min(mn, cap[t[nod]][nod]);
nod = t[nod];
}
if (mn <= 0)
{
continue;
}
flux += mn;
nod = n;
while (nod != 1)
{
cap[t[nod]][nod] -= mn;
cap[nod][t[nod]] += mn;
nod = t[nod];
}
}
}
out << flux;
in.close();
out.close();
return 0;
}