Cod sursa(job #2944827)

Utilizator vladburacBurac Vlad vladburac Data 22 noiembrie 2022 23:45:22
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1000;

ifstream fin( "maxflow.in" );
ofstream fout( "maxflow.out" );

vector <int> edges[NMAX+1];
int capacity[NMAX+1][NMAX+1];

int source, dest;
int parent[NMAX+1];

queue <int> q;

void bfs( int node ) {
  while( !q.empty() )
    q.pop();
  q.push( source );
  while( !q.empty() ) {
    auto qfront = q.front();
    q.pop();
    if( qfront == dest )
      return;
    for( auto vec: edges[qfront] ) {
      if( !parent[vec] && capacity[qfront][vec] ) {
        parent[vec] = qfront;
        q.push( vec );
      }
    }
  }
}
int main() {
  int n, m, a, b, c, node, flow, i, new_flow;
  fin >> n >> m;
  for( i = 0; i < m; i++ ) {
    fin >> a >> b >> c;
    capacity[a][b] = c;
    edges[a].push_back( b );
    edges[b].push_back( a );
  }
  source = 1;
  dest = n;
  parent[source] = -1;
  bfs( source );
  flow = 0;
  while( parent[dest] ) {
    node = dest;
    new_flow = 1e9;
    while( node != source ) {
      new_flow = min( new_flow, capacity[parent[node]][node] );
      node = parent[node];
    }
    node = dest;
    while( node != source ) {
      capacity[parent[node]][node] -= new_flow;
      capacity[node][parent[node]] += new_flow;
      node = parent[node];
    }
    flow += new_flow;
    for( i = 1; i <= n; i++ )
      parent[i] = 0;
    parent[source] = -1;
    bfs( source );
  }
  fout << flow;
  return 0;
}