Pagini recente » Cod sursa (job #1245870) | Cod sursa (job #730258) | Cod sursa (job #3289488) | Cod sursa (job #2895389) | Cod sursa (job #247163)
Cod sursa(job #247163)
#include <stdio.h>
#include <string.h>
long a[1010][1010], b[1010][1010], prec[1010];
int n, m;
long flux_max(int s, int d);
bool bf(int s, int d);
long minim(long a, long b);
int main()
{
int t1, t2, i;
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
scanf("%d%d", &n, &m);
for (i=0; i<m; i++)
{
scanf("%d%d", &t1, &t2);
scanf("%ld", &a[t1][t2]);
}//for i
printf("%ld", flux_max(1, n));
return 0;
}//main
long flux_max(int s, int d)
{
long f=0, min;
int i;
while (bf(s, d))
{
min=120000;
i=d;
while (i!=s)
{
min=minim(min, a[prec[i]][i]-b[prec[i]][i]);
i=prec[i];
}//while
i=d;
while (i!=s)
{
b[prec[i]][i]+=min;
b[i][prec[i]]-=min;
i=prec[i];
}//while
f+=min;
}//while
return f;
}//flux_max
bool bf(int s, int d)
{
int p, u, coada[5010], t, i;
memset(prec, -1, sizeof(prec));
coada[0]=s;
prec[s]=-2;
p=0;
u=0;
while (p<=u)
{
t=coada[p++];
for (i=1; i<=n; i++)
if ((prec[i]==-1)&&((a[t][i]-b[t][i])>0))
{
coada[++u]=i;
prec[i]=t;
if (i==d)
return 1;
}//if
}//while
return 0;
}//bf
long minim(long a, long b)
{
if (a<b)
return a;
else
return b;
}//minim