Pagini recente » Cod sursa (job #327943) | Cod sursa (job #2303408) | Cod sursa (job #1550918) | Cod sursa (job #1867089) | Cod sursa (job #275700)
Cod sursa(job #275700)
#include <stdio.h>
#include <string.h>
#define nmax 1005
#define inf 110005
int n, m, t [nmax], c [nmax] [nmax], f [nmax] [nmax], l [nmax] [nmax<<1];
inline int min (int a, int b)
{
if (a < b)
return a;
return b;
}
void scan ()
{
int i, x, y, cap;
scanf ("%d%d", &n, &m);
for (i=1; i <= m; ++i)
{
scanf ("%d%d%d", &x, &y, &cap);
c [x] [y]=c [y] [x]=cap;
l [x] [++l [x] [0]]=y;
l [y] [++l [y] [0]]=x;
}
}
int BFS ()
{
int p, u, i, nod, q [nmax];
memset (t, 0, sizeof (t));
p=u=1;
q [p]=1;
t [1]=-1;
while (p <= u)
{
nod=q [p];
for (i=1; i <= l [nod] [0]; ++i)
if ((!t [l [nod] [i]]) && (c [nod] [l [nod] [i]] - f [nod] [l [nod] [i]] > 0))
{
t [l [nod] [i]]=nod;
q [++u]=l [nod] [i];
if (q [u] == n)
return 1;
}
++p;
}
return 0;
}
int flux ()
{
int F, i, cr;
for (F=0; BFS (); F+=cr)
{
cr=inf;
for (i=n; i != 1; i=t [i])
cr=min (cr, c [t [i]] [i]-f [t [i]] [i]);
for (i=n; i != 1; i=t [i])
{
f [t [i]] [i]+=cr;
f [i] [t [i]]-=cr;
}
}
return F;
}
int main ()
{
freopen ("maxflow.in", "r", stdin);
freopen ("maxflow.out", "w", stdout);
scan ();
printf ("%d\n", flux ());
return 0;
}