Pagini recente » Cod sursa (job #2913846) | Statistici Gimi gugustiucul (gimi_gugustiucu) | Profil margiki | Cod sursa (job #3133600) | Cod sursa (job #572261)
Cod sursa(job #572261)
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
#include<ctype.h>
using namespace std;
#define dim 1001
#define pb push_back
#define INF 0x3f3f3f3f
#define buf 2500
int c[dim][dim], f[dim][dim];
int viz[dim] ,t[dim] , n , m ,poz;
vector <int> v[dim];
queue <int> q;
char ch[buf];
void r ( int &x )
{
x = 0;
while ( !isdigit ( ch[poz]))
{
poz++;
if ( poz >=buf )
fread ( ch ,1, buf-1 , stdin) , poz = 0;
}
while ( isdigit ( ch[poz]) )
{
x = x*10 + ch[poz]-'0';
poz ++;
if ( poz >=buf )
fread ( ch ,1, buf-1 , stdin) , poz = 0;
}
//printf("%d ",x);
}
void read()
{
int x,y,cost;
// scanf("%d %d\n",&n,&m);
r ( n ) ;
r ( 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])
continue;
if ( viz[y] == 0)
{
viz[ y ] == 1;
t [ y ] = x;
q.push ( y ) ;
if ( y == n )
{
while ( !q.empty() )
q.pop();
return 1;
}
}
}
}
return 0;
}
inline int min (int x, int y ) { if (x<y) return x ; return y;}
void solve()
{
int fmin,fmax=0;
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[ nod ] [ t[nod] ]-=fmin;
f [ t[nod] ] [ nod ]+=fmin;
}
fmax+=fmin;
}
printf( "%d ",fmax ) ;
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
fread ( ch ,1, buf-1 , stdin) , poz = 0;
read();
solve();
return 0;
}