Pagini recente » Cod sursa (job #1390450) | Cod sursa (job #1533233) | Cod sursa (job #1608134) | Cod sursa (job #1357936) | Cod sursa (job #2966412)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
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], t[max_size], n;
vector <int> mc[max_size];
queue <int> q;
bitset <max_size> viz;
bool bfs ()
{
queue <int> q;
for (int i = 1; i <= n; i++)
{
viz[i] = 0;
t[i] = 0;
}
viz[1] = 1;
q.push(1);
while (!q.empty())
{
int nod = q.front();
q.pop();
for (auto f : mc[nod])
{
if (!viz[f] && cap[nod][f] > 0)
{
t[f] = nod;
viz[f] = 1;
if (f == n)
{
return true;
}
q.push(f);
}
}
}
return false;
}
int main ()
{
int m;
in >> n >> m;
while (m--)
{
int x, y, z;
in >> x >> y >> z;
cap[x][y] += z;
mc[x].push_back(y);
mc[y].push_back(x);
}
int flux = 0;
while (bfs())
{
for (auto f : mc[n])
{
if (cap[f][n] <= 0 || !viz[f])
continue;
t[n] = f;
int mn = INF, nod = n;
while (nod != 1)
{
mn = min(mn, cap[t[nod]][nod]);
nod = t[nod];
}
if (mn == 0)
continue;
nod = n;
while (nod != 1)
{
cap[t[nod]][nod] -= mn;
cap[nod][t[nod]] += mn;
nod = t[nod];
}
flux += mn;
}
}
out << flux;
in.close();
out.close();
return 0;
}