Pagini recente » Cod sursa (job #360632) | Cod sursa (job #1211766) | Cod sursa (job #649512) | Cod sursa (job #1999258) | Cod sursa (job #531134)
Cod sursa(job #531134)
#include <cstdio>
#include <vector>
using namespace std;
#define nmax 1010
vector <int> g[nmax];
int n, m, c[nmax][nmax], sol, u[nmax], fm, q[nmax], f[nmax][nmax], t[nmax];
int bf()
{
int i, y, j, h=1;
for (i=1; i<=n; i++) u[i]=0;
q[1]=1;
u[1]=1;
for (y=1; y<=h; y++)
{
i=q[y];
if (i!=n)
for (j=0; j<g[i].size(); j++)
if (!u[g[i][j]])
if (c[i][g[i][j]]!=f[i][g[i][j]])
{
u[g[i][j]]=1;
q[++h]=g[i][j];
t[g[i][j]]=i;
}
}
return u[n];
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d %d",&n, &m);
int i, x, y, z, nod;
for (i=1; i<=m; i++)
{
scanf("%d %d %d",&x, &y, &z);
g[x].push_back(y);
g[y].push_back(x);
c[x][y] += z;
}
while (bf())
for (i=0; i<g[n].size(); i++)
if (u[g[n][i]])
if (f[g[n][i]][n] != c[g[n][i]][n])
{
t[n]=g[n][i];
nod=n;
fm=1<<30;
while (t[nod])
{
fm = min(fm, c[t[nod]][nod]-f[t[nod]][nod]);
nod=t[nod];
}
nod=n;
while (t[nod])
{
f[t[nod]][nod] += fm;
f[nod][t[nod]] -= fm;
nod=t[nod];
}
sol+=fm;
}
printf("%d\n",sol);
}