Pagini recente » Cod sursa (job #1666248) | Cod sursa (job #2580894) | Cod sursa (job #1490701) | Borderou de evaluare (job #1034237) | Cod sursa (job #571647)
Cod sursa(job #571647)
#include<stdio.h>
#include<vector>
#include<queue>
#include<bitset>
#include<string.h>
#define dim 1010
#define pb push_back
#define INF 0x3f3f3f3f
using namespace std;
vector < int > v[dim];
queue < int > q;
int c[dim][dim];
int f[dim][dim];
int n,m;
int t[dim];
int viz[dim];
void read()
{
scanf("%d %d\n",&n,&m);
int x,y,cost;
for(int i=1 ;i<=m;i++)
{
scanf("%d %d %d\n",&x,&y,&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 ] == 1 || c[x][y] == f[x][y] )
continue;
viz[ y ] = 1;
t [ y ] = x ;
q.push(y);
if ( y == n )
{
while ( !q.empty() )
q.pop();
return 1;
}
}
}
return 0;
}
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;
}