Pagini recente » Cod sursa (job #152364) | Cod sursa (job #209976) | Cod sursa (job #175025) | Cod sursa (job #1532389) | Cod sursa (job #245576)
Cod sursa(job #245576)
#include<cstdio>
#include<vector>
#include<queue>
#define NMAX 1024
#define inf 0x3f3f3f3f
using namespace std;
vector< int >G[NMAX];
int C[NMAX][NMAX],F[NMAX][NMAX];
int Q[NMAX],T[NMAX];
bool viz[NMAX];
int N,M;
bool BF()
{
int nod;
vector< int >::iterator it;
int inc=0,sf=1;
Q[1]=1;
memset( viz, 0 , sizeof( viz ) );
memset( T, 0, sizeof( T ) );
int debug=0;
viz[1]=1;
while( inc!=sf )
{
if( Q[++inc]==N ) continue;
nod=Q[inc];
for(it=G[nod].begin(); it!=G[nod].end(); ++it)
{
if( viz[ *it ] || C[ nod ][ *it ]==F[ nod ][ *it ] ) continue;
viz[ *it ]=1; Q[ ++sf ]=*it; T[ *it ]=nod;
}
//++debug;
//printf("%d %d\n",inc,sf);
//if( debug==4 )
//return 0;
}
return viz[N];
}
void flux()
{
vector< int >::iterator it;
int nod,flow,aux;
int ANS=0;
while( BF() )
{
for( it=G[N].begin(); it!=G[N].end(); ++it )
{
if( !viz[ *it ] || C[ *it ][ N ]==F[ *it ][ N ] ) continue;
T[N]=*it;flow=inf;
for( nod=N; nod!=1; nod=T[nod] )
{
aux=C[ T[nod] ][ nod ]-F[ T[nod] ][ nod ];
flow=flow<aux?flow:aux;
}
for( nod=N; nod!=1; nod=T[nod] )
{
F[ nod ][ T[ nod ] ]-=flow;
F[ T[ nod ] ][ nod ]+=flow;
}
ANS+=flow;
}
}
printf("%d\n",ANS);
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d%d",&N,&M);
int i,a1,a2,a3;
for(i=1; i<=M; ++i){
scanf("%d%d%d",&a1,&a2,&a3);
G[a1].push_back( a2 );
G[a2].push_back( a1 );
C[ a1 ][ a2 ]+=a3;
//C[ a2 ][ a1 ]+=a3;
}
flux();
return 0;
}//VS is sloow..