Pagini recente » Cod sursa (job #1432080) | Cod sursa (job #241190)
Cod sursa(job #241190)
/*
ID: gabitzish
LANG: C++
TASK: flow
*/
#include <stdio.h>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
int M, N;
int C[1005][1005], F[1005][1005];
int TT[1005], cd[1005], viz[1005];
typedef struct nod
{
int x, c;
nod *a;
} *pNod;
pNod v[100];
void add(int x, int y, int c)
{
pNod q = new nod;
q -> x = y;
q -> c = c;
q -> a = v[x];
v[x] = q;
}
int BF()
{
int i, nod, V;
cd[0] = 1;
cd[1] = 1;
memset(viz, 0, sizeof(viz));
viz[1] = 1;
for (i = 1; i <= cd[0]; i++)
{
nod = cd[i];
if (nod == N) continue;
for (pNod q = v[nod]; q != NULL; q = q -> a)
{
V = q -> x;
if (C[nod][V] == F[nod][V] || viz[V]) continue;
viz[V] = 1;
cd[ ++cd[0] ] = V;
TT[V] = nod;
}
}
return viz[N];
}
int min(int x, int y) { return x < y ? x : y;}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
int i, c, x, y, fmin, nod;
scanf("%d %d\n",&N, &M);
for (i = 1; i <= M; i++)
{
scanf("%d %d %d\n", &x, &y, &c);
C[x][y] += c;
add(x, y, c); add(y, x, c);
}
int flux = 0;
while (BF())
{
for (pNod q = v[N]; q != NULL; q = q -> a)
{
nod = q -> x;
if (F[nod][N] == C[nod][N] || !viz[nod]) continue;
TT[N] = nod;
fmin = INF;
for (nod = N; nod != 1; nod = TT[nod])
fmin = min(fmin, C[ TT[nod] ][nod] - F[ TT[nod] ][nod]);
if (fmin == 0) continue;
for (nod = N; nod != 1; nod = TT[nod])
{
F[ TT[nod] ][nod] += fmin;
F[nod][ TT[nod] ] -= fmin;
}
flux += fmin;
}
}
printf("%d\n",flux);
return 0;
}