Pagini recente » Cod sursa (job #2166280) | Cod sursa (job #2038973) | Cod sursa (job #2875479) | Cod sursa (job #2664692) | Cod sursa (job #3351110)
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e3;
int cap[nmax + 1][nmax + 1], flow[nmax + 1][nmax + 1], t[nmax + 1];
vector <int> g[nmax + 1];
queue <int> q, sol;
bool ok[nmax + 1];
int maxflow, nod, x, maxpush;
bool solve(int n)
{
memset(ok, 0, n);
for(auto x : g[1])
{
if(!ok[x] && flow[1][x] < cap[1][x])
{
ok[x] = 1;
t[x] = 1;
q.push(x);
}
}
while(!q.empty())
{
nod = q.front();
q.pop();
for(auto x : g[nod])
{
if(!ok[x] && flow[nod][x] < cap[nod][x])
{
if(x == n)
sol.push(nod);
else
{
ok[x] = 1;
t[x] = nod;
q.push(x);
}
}
}
}
if(!sol.empty())
{
while(!sol.empty())
{
x = sol.front();
sol.pop();
maxpush = cap[x][n] - flow[x][n];
for(nod = x; nod != 1 && maxpush > 0; nod = t[nod])
maxpush = min(maxpush, cap[t[nod]][nod] - flow[t[nod]][nod]);
if(maxpush > 0)
{
maxflow += maxpush;
flow[x][n] += maxpush;
for(nod = x; nod != 1 && maxpush > 0; nod = t[nod])
{
flow[t[nod]][nod] += maxpush;
flow[nod][t[nod]] -= maxpush;
}
}
}
return 1;
}
return 0;
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, x, y, z, i;
cin >> n >> m;
for(i = 1; i <= m; i++)
{
cin >> x >> y >> z;
g[x].push_back(y);
g[y].push_back(x);
cap[x][y] = z;
}
while(solve(n));
cout << maxflow;
return 0;
}