Pagini recente » Cod sursa (job #2186233) | Cod sursa (job #2687600) | Cod sursa (job #1828945) | Cod sursa (job #3263609) | Cod sursa (job #572274)
Cod sursa(job #572274)
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
#include<ctype.h>
using namespace std;
#define dim 1010
#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] ) )
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;
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[ t[nod] ][ nod ]+=fmin;
f[ nod ][ t[nod] ]-=fmin;
}
fmax+=fmin;
}
printf( "%d ",fmax ) ;
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
fread (ch , 1 , buf , stdin) , poz = 0;
read();
solve();
return 0;
}