Pagini recente » Cod sursa (job #975717) | Cod sursa (job #2634183) | Cod sursa (job #416851) | Cod sursa (job #1852972) | Cod sursa (job #409693)
Cod sursa(job #409693)
#include <queue>
#include <vector>
#include <fstream>
#define Nmax 1000
#define INF 0x3f3f3f3f
/*
*
*/
using namespace std;
int N, source, sink;
int C[Nmax][Nmax], Cost[Nmax][Nmax];
queue< int > Q;
vector< bool > inQ;
vector< int > d, c, father;
vector< vector< int > > G;
vector< int >::const_iterator it, iend;
int find_path( void )
{
int x;
d.assign( N, INF );
father.assign( N, INF );
d[source]=0;
c[source]=INF;
Q.push( source );
while( !Q.empty() )
{
x=Q.front(); Q.pop();
inQ[x]=true;
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
if( C[x][*it] > 0 && d[x]+Cost[x][*it] < d[*it] )
{
father[*it]=x;
d[*it]=d[x]+Cost[x][*it];
c[*it]=min( c[x], C[x][*it] );
if( !inQ[*it] )
{
Q.push( *it );
inQ[*it]=true;
}
}
}
if( INF == father[sink] )
return INF;
father[source]=INF;
for( x=sink; INF != father[x]; x=father[x] )
{
C[father[x]][x]-=c[sink];
if( 0 == C[x][father[x]] )
{
G[x].push_back( father[x] );
Cost[x][father[x]]=-Cost[father[x]][x];
}
C[x][father[x]]+=c[sink];
}
return c[sink]*d[sink];
}
int MaxFlowMinCut( void )
{
int path_c, s=0;
for( path_c=find_path(); INF != path_c; s+=path_c, path_c=find_path() );
return s;
}
int main( void )
{
int M, x, y;
ifstream in( "fmcm.in" );
in>>N>>M>>source>>sink;
--source, --sink;
G.resize(N);
c.resize(N);
d.resize(N);
inQ.resize(N);
father.resize(N);
for( ; M; --M )
{
in>>x>>y;
--x, --y;
in>>C[x][y]>>Cost[x][y];
G[x].push_back(y);
}
ofstream out( "fmcm.out" );
out<<MaxFlowMinCut();
return 0;
}