Problem p02376 (C++)

Buggy submission (status: Time Limit Exceeded):
#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);
    q = {from, 0, int(G[from].size() - 1)};
    G[to].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;
  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;
}


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);
    q = {from, 0, int(G[from].size() - 1)};
    G[to].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;
}