Pagini recente » Cod sursa (job #1356236) | Cod sursa (job #2328132) | Cod sursa (job #2243538) | Cod sursa (job #860599) | Cod sursa (job #474190)
Cod sursa(job #474190)
// Flux Maximal.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <stdio.h>
#include "string.h"
#define nmax 1024
FILE *ff=fopen("maxflow.in", "r");
FILE *g=fopen("maxflow.out", "w");
int c[nmax][nmax];
int f[nmax][nmax];
int v[nmax][nmax];
int ant[nmax];
int viz[nmax];
int cd[5000];
int n, m;
void read()
{
fscanf(ff, "%d%d", &n, &m);
for (int i=1; i<=m; ++i)
{
int x, y, d;
fscanf(ff, "%d%d%d", &x, &y, &d);
c[x][y]+=d;
v[x][++v[x][0]]=y;
v[y][++v[y][0]]=x;
}
}
int bfs()
{
memset(viz, 0, sizeof(viz));
cd[0]=1;
cd[1]=1;
for (int i=1; i<=cd[0]; ++i)
{
int nod=cd[i];
viz[nod]=1;
for (int j=1; j<=v[nod][0]; ++j)
{
int vf=v[nod][j];
if (!viz[vf] && c[nod][vf]>f[nod][vf])
{
cd[++cd[0]]=vf;
ant[vf]=nod;
if (vf==n) return 1;
}
}
}
return 0;
}
int minim(int x, int y)
{
if (x<y) return x;
return y;
}
void program()
{
int fmin=0;
int flow;
for (flow=0; bfs(); flow+=fmin)
{
fmin=100000000;
for (int nod=n; nod>1; nod=ant[nod])
fmin=minim(fmin, c[ant[nod]][nod]-f[ant[nod]][nod]);
for (int nn=n; nn>1; nn=ant[nn])
{
f[ant[nn]][nn]+=fmin;
f[nn][ant[nn]]-=fmin;
}
}
fprintf(g, "%d", flow);
}
int main()
{
read();
program();
return 0;
}