Pagini recente » Cod sursa (job #2990952) | Cod sursa (job #1715074) | Cod sursa (job #1501868) | Cod sursa (job #2416946) | Cod sursa (job #2568800)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int nmax = 1005;
const int inf = (1<<30);
int n, m, ft;
int ca[nmax][nmax], fl[nmax][nmax], p[nmax];
vector < int > l[nmax];
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
l[x].push_back(y);
l[y].push_back(x);
ca[x][y] += c;
}
do
{
for (int i = 1; i <= n; i++)
p[i] = inf;
queue < int > q;
q.push(1);
while (!q.empty())
{
int x = q.front();
q.pop();
for (int el : l[x])
{
if (p[el] == inf && fl[x][el] < ca[x][el])
{
p[el] = x;
q.push(el);
}
}
}
if (p[n] != inf)
{
int x = n, fm = inf;
while (x != 1)
{
fm = min(fm, ca[p[x]][x] - fl[p[x]][x]);
x = p[x];
}
x = n;
while (x != 1)
{
fl[p[x]][x] += fm;
fl[x][p[x]] -= fm;
x = p[x];
}
ft += fm;
}
}
while (p[n] != inf);
fout << ft;
fin.close();
fout.close();
return 0;
}