Problem p01903 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 10000000                // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

struct Dinic {

  struct edge {
    int to, cap, rev;
    edge(int a, int b, int c) : to(a), cap(b), rev(c) {}
  };

  vector<vector<edge>> G;
  vector<int> level, iter;
  int MAX_V;
  int last_solve;

  Dinic(int max_v) : G(max_v, vector<edge>()), MAX_V(max_v), last_solve(0) {}

  int add_edge(int from, int to, int cap) {
    int id = G[from].size();

    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size() - 1));

    return id;
  }

  void bfs(int s) {
    level.assign(MAX_V, -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (q.size()) {
      int v = q.front();
      q.pop();
      for (int i = 0; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          q.push(e.to);
        }
      }
    }
  }

  int dfs(int v, int t, int f) {
    if (v == t)
      return f;
    for (int &i = iter[v]; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  int solve(int s, int t) {
    int flow = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0) {
        last_solve += flow;
        return last_solve;
      }
      iter.assign(MAX_V, 0);
      int f;
      while ((f = dfs(s, t, INF)) > 0)
        flow += f;
    }
  }
};

int k, n, m;
int a[SIZE], b[SIZE], c[SIZE];

Dinic calc_first(queue<pair<int, pair<int, int>>> &q) {
  Dinic dinic(102);
  vector<pair<int, pair<int, int>>> vec;

  for (int i = 0; i < m; i++) {
    int id = dinic.add_edge(a[i], b[i], c[i]);
    int id2 = dinic.add_edge(b[i], a[i], c[i]);
    vec.push_back({i, {id, id2}});
  }

  for (int i = 2; i <= k + 1; i++) {
    dinic.add_edge(0, i, INF);
  }

  debug(dinic.solve(0, 1));

  for (int i = 0; i < vec.size(); i++) {
    if (dinic.G[a[vec[i].first]][vec[i].second.first].cap == 0 ||
        dinic.G[b[vec[i].first]][vec[i].second.second].cap == 0) {
      q.push(vec[i]);
    }
  }

  return dinic;
}

int main() {

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

  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", a + i, b + i, c + i);
    a[i]++;
    b[i]++;
  }

  int ans = 0;
  queue<pair<int, pair<int, int>>> q;

  Dinic dinic = calc_first(q);

  while (q.size()) {
    Dinic dinic2 = dinic;
    pair<int, pair<int, int>> t = q.front();
    q.pop();

    dinic2.G[a[t.first]][t.second.first].cap += INF;
    dinic2.G[b[t.first]][t.second.second].cap += INF;
    ans = max(ans, dinic2.solve(0, 1));
  }

  if (ans >= INF) {
    puts("overfuro");
  } else {
    printf("%d\n", ans);
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 10000000                // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

struct Dinic {

  struct edge {
    int to, cap, rev;
    edge(int a, int b, int c) : to(a), cap(b), rev(c) {}
  };

  vector<vector<edge>> G;
  vector<int> level, iter;
  int MAX_V;
  int last_solve;

  Dinic(int max_v) : G(max_v, vector<edge>()), MAX_V(max_v), last_solve(0) {}

  int add_edge(int from, int to, int cap) {
    int id = G[from].size();

    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size() - 1));

    return id;
  }

  void bfs(int s) {
    level.assign(MAX_V, -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (q.size()) {
      int v = q.front();
      q.pop();
      for (int i = 0; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          q.push(e.to);
        }
      }
    }
  }

  int dfs(int v, int t, int f) {
    if (v == t)
      return f;
    for (int &i = iter[v]; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  int solve(int s, int t) {
    int flow = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0) {
        last_solve += flow;
        return last_solve;
      }
      iter.assign(MAX_V, 0);
      int f;
      while ((f = dfs(s, t, INF)) > 0)
        flow += f;
    }
  }
};

int k, n, m;
int a[SIZE], b[SIZE], c[SIZE];

Dinic calc_first(queue<pair<int, pair<int, int>>> &q) {
  Dinic dinic(102);
  vector<pair<int, pair<int, int>>> vec;

  for (int i = 0; i < m; i++) {
    int id = dinic.add_edge(a[i], b[i], c[i]);
    int id2 = dinic.add_edge(b[i], a[i], c[i]);
    vec.push_back({i, {id, id2}});
  }

  for (int i = 2; i <= k + 1; i++) {
    dinic.add_edge(0, i, INF);
  }

  dinic.solve(0, 1);

  for (int i = 0; i < vec.size(); i++) {
    if (dinic.G[a[vec[i].first]][vec[i].second.first].cap == 0 ||
        dinic.G[b[vec[i].first]][vec[i].second.second].cap == 0) {
      q.push(vec[i]);
    }
  }

  return dinic;
}

int main() {

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

  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", a + i, b + i, c + i);
    a[i]++;
    b[i]++;
  }

  int ans = 0;
  queue<pair<int, pair<int, int>>> q;

  Dinic dinic = calc_first(q);

  while (q.size()) {
    Dinic dinic2 = dinic;
    pair<int, pair<int, int>> t = q.front();
    q.pop();

    dinic2.G[a[t.first]][t.second.first].cap += INF;
    dinic2.G[b[t.first]][t.second.second].cap += INF;
    ans = max(ans, dinic2.solve(0, 1));
  }

  if (ans >= INF) {
    puts("overfuro");
  } else {
    printf("%d\n", ans);
  }

  return 0;
}