Pagini recente » Cod sursa (job #103376) | Profil FreeYourMind | Cod sursa (job #555064) | Profil maritim | Cod sursa (job #572284)
Cod sursa(job #572284)
#include<stdio.h>
#include<vector>
#include<queue>
#include<bitset>
#include<string.h>
#define dim 1010
#define pb push_back
#define buf 150
#define INF 0x3f3f3f3f
using namespace std;
vector < int > v[dim];
queue < int > q;
char ch [buf];
int c[dim][dim];
int f[dim][dim];
int n,m;
int t[dim];
int viz[dim],poz;
void r ( int &x )
{
x = 0;
while ( !isdigit ( ch[poz] ) )
if ( ++poz == buf )
fread (ch , 1 , buf , stdin) , poz = 0;
while ( isdigit ( ch[poz]) )
{
x = x*10 + ch[poz] - '0';
if ( ++poz == buf )
fread (ch , 1 , buf , stdin) , poz = 0;
}
//printf("%d ",x);
}
void read()
{int x,y,cost;
r ( n ) ;
r ( m ) ;
// scanf("%d %d\n",&n,&m);
for(int i=1; i<=m;i++)
{
// scanf("%d %d %d\n",&x,&y,&cost);
r ( x ) ;
r ( y ) ;
r ( cost ) ;
c[x][y]+=cost;
v[x].pb ( y );
v[y].pb ( x );
}
}
int bf ()
{
int x,y;
memset(viz,0,sizeof(viz));
for(q.push(1) ; !q.empty() ; q.pop() )
{
x = q.front ();
for(int i=0 ; i<v[x].size() ; i++ )
{
y = v[x][i];
if ( viz [ y ] ==0 && c[x][y] != f[x][y] )
{
viz[ y ] = 1;
t [ y ] = x ;
if ( y!=n )
q.push(y);
}
}
}
return viz[n];
}
int min ( int x, int y )
{
if ( x<y)
return x;
return y;
}
void solve()
{
int flux = 0,fmin;
while ( bf ( ) )
{
fmin = INF;
for(int nod = n ; nod!=1 ; nod = t[ nod ] )
fmin = min (fmin , c[ t[nod] ][ nod ] -f[ t[nod] ][ nod ] );
for(int nod = n ; nod!=1 ; nod = t[nod] )
{
f[t[nod]][nod]+=fmin;
f[nod][t[nod]]-=fmin;
}
flux+=fmin;
}
printf("%d\n",flux);
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
read();
solve();
return 0;
}