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


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9 : 1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
  int b = 0;
  for (auto itr : a) {
    if (b++ != 0)
      cout << " ";
    cout << itr;
  }
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
struct Edge {
  int to, cap, rev;
};
class Dinic { // max flow
public:
  int n;
  vector<vector<Edge>> G;
  vector<int> level, iter;
  Dinic(int size) {
    n = size;
    G = vector<vector<Edge>>(n);
  }
  void add_Edge(int from, int to, int cap) {
    // rev は逆辺にアクセスできる
    // e = G[from][index]
    // G[e.to][e.rev].to = from,.rev = index
    Edge q = {to, cap, int(G[to].size())};
    G[from].emplace_back(q);
    q = {from, 0, int(G[from].size() - 1)};
    G[to].emplace_back(q);
  }
  void bfs(int s) {
    level = vector<int>(n, -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      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 (level[v] >= level[e.to] || e.cap <= 0)
        continue;
      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 max_flow(int s, int t) { // from s to t,ford_fulkerson
    int flow = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      iter = vector<int>(n);
      int f;
      while ((f = dfs(s, t, INF)) > 0)
        flow += f;
    }
  }
};
signed main() {
  int v, e;
  cin >> v >> e;
  Dinic yebi(v + 1);
  rep(i, e) {
    int a, b, c;
    cin >> a >> b >> c;
    yebi.add_Edge(a, b, c);
  }
  cout << yebi.max_flow(0, v - 1) << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
using ll = long long;
using ld = long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int W, H;
bool inrange(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
#define MP make_pair
struct edge {
  int to, cap, rev;
};            // ikisaki youryou gyakuhen
class Dinic { // max flow
public:
  int n;
  vector<vector<edge>> G; //[MAX];
  vi level, iter;         //[MAX];
  Dinic(int size) {
    n = size;
    G = vector<vector<edge>>(n);
  }
  void add_edge(int from, int to, int cap) {
    edge q = {to, cap, int(G[to].size())};
    G[from].push_back(q);
  }
  int getmaxflow(int s, int t) { // from s to t,ford_fulkerson
    int flow = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      iter = vi(n);
      int f;
      while ((f = dfs(s, t, INF)) > 0)
        flow += f;
    }
  }

private:
  void bfs(int s) {
    level = vi(n, -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      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 (level[v] >= level[e.to] || e.cap <= 0)
        continue;
      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;
  }
};

signed main() {
  int v, e;
  cin >> v >> e;
  Dinic yebi(v + 1);
  rep(i, e) {
    int a, b, c;
    cin >> a >> b >> c;
    yebi.add_edge(a, b, c);
  }
  cout << yebi.getmaxflow(0, v - 1) << endl;
}