Pagini recente » Cod sursa (job #2386347) | Cod sursa (job #3199213) | Cod sursa (job #2126838) | Cod sursa (job #401571) | Cod sursa (job #563099)
Cod sursa(job #563099)
#include<stdio.h>
#include<vector>
#define maxN 1005
#define Srs 1
#define Dst N
using namespace std;
FILE*f=fopen("maxflow.in","r");
FILE*g=fopen("maxflow.out","w");
int N,M,i,x,y,c;
int Cp[maxN][maxN],F[maxN][maxN],T[maxN],Viz[maxN],C[maxN];
vector<int>A[maxN];
int Bfs () {
int nod,p,u,nodvc,ok = 0;
p = u = 1;
C[1] = Srs;
memset(Viz,0,sizeof(Viz));
Viz[Srs] = 1;
while ( p <= u ){
nod = C[p];
for ( int i = 0 ; i < A[nod].size() ; ++i ){
nodvc = A[nod][i];
if ( Viz[nodvc] || Cp[nod][nodvc] <= F[nod][nodvc] ) continue;
if ( nodvc == Dst ){
ok = 1;
continue;
}
C[++u] = nodvc;
Viz[nodvc] = 1;
T[nodvc] = nod;
}
++p;
}
return ok;
}
int maxflow () {
int nod,nodaux,minim,fluxmaxim = 0;
while ( Bfs() ){
for ( int i = 0 ; i < A[Dst].size() ; ++i ){
nod = A[Dst][i];
minim = Cp[nod][Dst] - F[nod][Dst];
nodaux = nod;
while ( T[nodaux] ){
minim = min(minim,Cp[T[nodaux]][nodaux] - F[T[nodaux]][nodaux]);
nodaux = T[nodaux];
}
nodaux = Dst;
T[Dst] = nod;
while ( T[nodaux] ){
F[T[nodaux]][nodaux] += minim;
F[nodaux][T[nodaux]] -= minim;
nodaux = T[nodaux];
}
fluxmaxim += minim;
}
}
return fluxmaxim;
}
int main () {
fscanf(f,"%d %d",&N,&M);
for ( i = 1 ; i <= M ; ++i ){
fscanf(f,"%d %d %d",&x,&y,&c);
A[x].push_back(y);
A[y].push_back(x);
Cp[x][y] = c;
}
fprintf(g,"%d\n", maxflow());
fclose(f);
fclose(g);
return 0;
}