Cod sursa(job #2545853)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 13 februarie 2020 16:43:08
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.28 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <queue>

using namespace std;

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

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
using ll = long long;

int const nmax = 100000;
int const inf = 1000000000;

class Graph{
private:
  struct Edge{
    int to;
    int flow;
    int cap;
    int rev;
  };
  vector<vector<Edge>> g;
  vector<int> level, rem;
  int n;
  int source, sink;
public:
  Graph(int n_){
    n = n_;
    g.resize(1 + n);
    level.resize(1 + n);
    rem.resize(1 + n);
    source = 1;
    sink = n;
  }
  void addedge(int x, int y, int cap){
    g[x].push_back({y, 0, cap, g[y].size()});
    g[y].push_back({x, 0, 0, g[x].size() - 1});
  }
  bool BFS(){
    for(int i = 1; i <= n; i++)
      level[i] = 0;
    level[source] = 1;
    queue<int> q;
    q.push(source);
    while(0 < q.size()) {
      int node = q.front();
      q.pop();
      for(int h = 0; h < g[node].size(); h++){
        Edge e = g[node][h];
        if(e.flow < e.cap && level[e.to] == 0){
          level[e.to] = level[node] + 1;
          q.push(e.to);
        }
      }
    }
    return 0 < level[sink];
  }

  int DFS(int node, int deltaflow){
    if(node == sink){
      return deltaflow;
    } else {
      for(int &h = rem[node]; h < g[node].size(); h++){
        Edge &e = g[node][h];
        if(level[node] + 1 == level[e.to] && e.flow < e.cap){
          int delta = DFS(e.to, MIN(deltaflow, e.cap - e.flow));
          if(0 < delta){
            e.flow += delta;
            g[e.to][e.rev].flow -= delta;
            return delta;
          }
        }
      }
      return 0;
    }
  }

  int maxflow(){
    int result = 0;
    while(BFS()){
      for(int i = 1;i <= n; i++)
        rem[i] = 0;
      int delta = 0;
      do {
        result += delta;
        delta = DFS(source, inf);
      } while(0 < delta);
    }
    return result;
  }
};

int main()
{
  int n, m;
  in >> n >> m;
  Graph graph(n);
  for(int i = 1;i <= m; i++){
    int x, y, cost;
    in >> x >> y >> cost;
    graph.addedge(x, y, cost);
  }
  out << graph.maxflow();
  return 0;
}