Cod sursa(job #2283834)

Utilizator isav_costinVlad Costin Andrei isav_costin Data 15 noiembrie 2018 22:18:38
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.64 kb
#include <cstdio>
#include <vector>
#include <queue>

#define MAXN 1000
#define INF 1000000

using namespace std;

int viz[MAXN+5], n, flow;
int flux[MAXN+5][MAXN+5]; // cat flux mai pot baga pe o muchie
int tata[MAXN+5];

vector<int> graph[MAXN+5];
queue<int> q;

inline int minim( int a, int b )
{
  if( a>b )
    a=b;

  return a;
}

inline void pump_flux( )
{
  int nod=n, fl=INF;

  while( nod!=1 )
  {
    fl=minim(fl,flux[tata[nod]][nod]);

    nod=tata[nod];
  }

  nod=n;

  while( nod!=1 )
  {
    flux[tata[nod]][nod]-=fl;
    flux[nod][tata[nod]]+=fl;

    nod=tata[nod];
  }

  flow+=fl;
}

inline int reset( int *v )
{
  for( int i=1;i<=n;i++ )
    viz[i]=0;
}

inline int still_flux( )
{
  int ok=0;

  reset(viz);

  q.push(1);
  viz[1]=1;

  while( !q.empty() )
  {
    int top=q.front();

    q.pop();

    for( vector<int>::iterator it=graph[top].begin();it!=graph[top].end();it++ )
    {
      if( flux[top][*it] && !viz[*it] && *it!=n )
      {
        tata[*it]=top;

        q.push(*it);
        viz[*it]=1;
      }
      else
        if( flux[top][*it] && *it==n )
        {
          tata[*it]=top;

          ok=1;
          pump_flux();
        }
    }
  }

  return ok;
}

int main()
{
  freopen( "maxflow.in", "r", stdin );
  freopen( "maxflow.out", "w", stdout );

  int m;

  scanf( "%d%d", &n, &m );

  for( int i=1;i<=m;i++ )
  {
    int x, y, z;

    scanf( "%d%d%d", &x, &y, &z );

    flux[x][y]=z;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }

  while(still_flux());

  printf( "%d", flow );

  return 0;
}