Cod sursa(job #2778875)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 2 octombrie 2021 12:52:57
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.95 kb
#include <bits/stdc++.h>

using namespace std;



class flow {
public:
  struct edge {
    int to;
    int cap;
    int nxt;
  };

  int n;
  vector<edge> edges;
  vector<int> level, pos, head;

  flow(int nn) : n(nn), level(n + 1), pos(n + 1), head(nn + 1, -1) {
  }

  void add(int a, int b, int c) {
    edges.push_back({b, c, head[a]});
    edges.push_back({a, 0, head[b]});


    head[a] = (int) edges.size() - 2;
    head[b] = (int) edges.size() - 1;
  }

  int dfs(int a, int cur) {
    if (a == n || !cur) return cur;
    while (pos[a] != -1) {
      int i = pos[a];
      pos[a] = edges[i].nxt;
     // cout << pos[a] << "\n";
      int b = edges[i].to, cap = edges[i].cap;
      if (level[b] == 1 + level[a] && cap > 0) {
        int x = dfs(b, min(cur, cap));
        if (x > 0) {
          edges[i].cap -= x;
          edges[i ^ 1].cap += x;
          return x;
        }
      }
    }
    return 0;
  }

  int get() {
    int sol = 0;
    while (1) {
      for (int i = 1; i <= n; i++) {
        level[i] = -1;
        pos[i] = head[i];
      }
      level[1] = 0;
      queue<int> q;
      q.push(1);
      while (!q.empty()) {
        int a = q.front();
        q.pop();
        for (int i = head[a]; i != -1; i = edges[i].nxt) {
          int b = edges[i].to;
          if (edges[i].cap && level[b] == -1) {
            level[b] = 1 + level[a];
            q.push(b);
          }
        }
      }

      if (level[n] == -1) break;

      while (1) {
        int nw = dfs(1, (int) 1e9);

        if (nw == 0) break;
        sol += nw;
      }
    }

    return sol;
  }

};

const int N = 1000 + 7;
int n, m;

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

  cin >> n >> m;
  flow f(n);
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    f.add(a, b, c);
  }
  cout << f.get() << "\n";

  return 0;
}