Problem p02376 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int inf = (1 << 28);

struct Network {
  struct Arc {
    int from, to, cap, cost, rev;

    Arc() {}
    Arc(int from, int to, int cap, int cost, int rev)
        : from(from), to(to), cap(cap), cost(cost), rev(rev) {}
  };

  int n;
  vector<vector<Arc>> graph;

  Network(int n = 0) : n(n), graph(n) {}

  void addArc(int from, int to, int cap) {
    graph[from].emplace_back(from, to, cap, 0, graph[to].size());
    graph[to].emplace_back(to, from, 0, 0, graph[from].size() - 1);
  }

  int findBlockingFlow(vector<Arc *> &hist, int s, int t) {
    vector<int> dist(n, inf), limit(n, inf);
    fill(hist.begin(), hist.end(), nullptr);

    queue<int> que;
    dist[s] = 0;
    que.push(s);

    while (!que.empty()) {
      int u = que.front();
      que.pop();

      for (Arc &arc : graph[u]) {
        if (dist[arc.to] > dist[u] + 1 and arc.cap > 0) {
          dist[arc.to] = dist[u] + 1;
          hist[arc.to] = &arc;
          limit[arc.to] = min(limit[u], arc.cap);
          que.push(arc.to);

          if (arc.to == t)
            break;
        }
      }
    }
    return limit[t];
  }

  int findMaximumFlow(int s, int t) {
    vector<Arc *> hist(n, nullptr);
    int sumFlow = 0, flow;

    while (flow = findBlockingFlow(hist, s, t), hist[t] != nullptr) {
      Arc *flowPtr = hist[t];
      while (flowPtr != nullptr) {
        flowPtr->cap -= flow;
        graph[flowPtr->to][flowPtr->rev].cap += flow;
        flowPtr = hist[flowPtr->from];
      }
      sumFlow += flow;
    }
    return sumFlow;
  }

  int findMinimumCut(int s, int t) { return findMaximumFlow(s, t); }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  Network network(n);
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    network.addArc(u, v, c);
  }
  cout << network.findMaximumFlow(0, n - 1) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int inf = (1 << 28);

struct Network {
  struct Arc {
    int from, to, cap, cost;
    Arc *rev;

    Arc() {}
    Arc(int from, int to, int cap, int cost, Arc *rev)
        : from(from), to(to), cap(cap), cost(cost), rev(rev) {}
  };

  int n;
  vector<vector<Arc>> graph;

  Network(int n = 0) : n(n), graph(n) {}

  void addArc(int from, int to, int cap) {
    graph[from].emplace_back(from, to, cap, 0, nullptr);
    graph[to].emplace_back(to, from, 0, 0, &graph[from].back());
    graph[from].back().rev = &graph[to].back();
  }

  int findBlockingFlow(vector<Arc *> &hist, int s, int t) {
    vector<int> dist(n, inf), limit(n, inf);
    fill(hist.begin(), hist.end(), nullptr);

    queue<int> que;
    dist[s] = 0;
    que.push(s);

    while (!que.empty()) {
      int u = que.front();
      que.pop();

      for (Arc &arc : graph[u]) {
        if (dist[arc.to] > dist[u] + 1 and arc.cap > 0) {
          dist[arc.to] = dist[u] + 1;
          hist[arc.to] = &arc;
          limit[arc.to] = min(limit[u], arc.cap);
          que.push(arc.to);

          if (arc.to == t)
            break;
        }
      }
    }
    return limit[t];
  }

  int findMaximumFlow(int s, int t) {
    vector<Arc *> hist(n, nullptr);
    int sumFlow = 0, flow;

    while (flow = findBlockingFlow(hist, s, t), hist[t] != nullptr) {
      Arc *flowPtr = hist[t];
      while (flowPtr != nullptr) {
        flowPtr->cap -= flow;
        flowPtr->rev->cap += flow;
        flowPtr = hist[flowPtr->from];
      }
      sumFlow += flow;
    }
    return sumFlow;
  }

  int findMinimumCut(int s, int t) { return findMaximumFlow(s, t); }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  Network network(n);
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    network.addArc(u, v, c);
  }
  cout << network.findMaximumFlow(0, n - 1) << endl;

  return 0;
}