Problem p02376 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

class Flow {
public:
  int N, en;
  vector<int> C, F;
  vector<vector<pii>> edge;
  Flow(int n, int e) : C(e + 1), F(e + 1), N(n), edge(n) { en = 0; }
  void addEdge(int f, int t, int c) {
    en++;
    edge[f].emplace_back(t, en);
    edge[t].emplace_back(f, -en);
    C[en] = c;
  }
  int flow(int n, int c, int T, vector<bool> bf) {
    if (n == T)
      return c;
    bf[n] = true;
    for (auto to : edge[n]) {
      int i = to.second;
      int cc = (i > 0 ? C[i] - F[i] : F[-i]);
      if (bf[to.first] || cc == 0)
        continue;
      int f = flow(to.first, min(c, cc), T, bf);
      if (f > 0) {
        F[abs(i)] += f * (i / abs(i));
        return f;
      }
    }
    return 0;
  }
  int maxFlow(int S, int T) {
    for (int ret = 0;;) {
      vector<bool> bf(N);
      int add = flow(0, INF, T, bf);
      ret += add;
      if (add == 0)
        return ret;
    }
  }
  void clear() { fill(F.begin(), F.end(), 0); }
};

int V, E;
int C[1005];
int F[1005];
vector<pii> e[105];
int solve(int n, int c, bool bf[105]) {
  if (n == V - 1)
    return c;
  bf[n] = true;
  for (auto to : e[n]) {
    int i = to.second;
    int cc = (i > 0 ? C[i] - F[i] : F[-i]);
    if (bf[to.first] || cc == 0)
      continue;
    int flow = solve(to.first, min(c, cc), bf);
    if (flow > 0) {
      F[abs(i)] += flow * (i / abs(i));
      return flow;
    }
  }
  return 0;
}

int main() {
  cin >> V >> E;
  Flow fl(V, E);
  for (int i = 1; i <= E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    //		e[u].emplace_back(v,  i);
    //		e[v].emplace_back(u, -i);
    //		C[i] = c;
    fl.addEdge(u, v, c);
  }
  //	int ans = 0;
  //	while(1){
  //		bool bf[105] = {};
  //		int add = solve(0, INF, bf);
  //		ans += add;
  //		if( add == 0 ) break;
  //	}

  cout << fl.maxFlow(0, V - 1) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

class Flow {
public:
  int N, en;
  vector<int> C, F;
  vector<vector<pii>> edge;
  Flow(int n, int e) : C(e + 1), F(e + 1), N(n), edge(n) { en = 0; }
  void addEdge(int f, int t, int c) {
    en++;
    edge[f].emplace_back(t, en);
    edge[t].emplace_back(f, -en);
    C[en] = c;
  }
  int flow(int n, int c, int T, vector<bool> &bf) {
    if (n == T)
      return c;
    bf[n] = true;
    for (auto to : edge[n]) {
      int i = to.second;
      int cc = (i > 0 ? C[i] - F[i] : F[-i]);
      if (bf[to.first] || cc == 0)
        continue;
      int f = flow(to.first, min(c, cc), T, bf);
      if (f > 0) {
        F[abs(i)] += f * (i / abs(i));
        return f;
      }
    }
    return 0;
  }
  int maxFlow(int S, int T) {
    for (int ret = 0;;) {
      vector<bool> bf(N);
      int add = flow(0, INF, T, bf);
      ret += add;
      if (add == 0)
        return ret;
    }
  }
  void clear() { fill(F.begin(), F.end(), 0); }
};

int V, E;
int C[1005];
int F[1005];
vector<pii> e[105];
int solve(int n, int c, bool bf[105]) {
  if (n == V - 1)
    return c;
  bf[n] = true;
  for (auto to : e[n]) {
    int i = to.second;
    int cc = (i > 0 ? C[i] - F[i] : F[-i]);
    if (bf[to.first] || cc == 0)
      continue;
    int flow = solve(to.first, min(c, cc), bf);
    if (flow > 0) {
      F[abs(i)] += flow * (i / abs(i));
      return flow;
    }
  }
  return 0;
}

int main() {
  cin >> V >> E;
  Flow fl(V, E);
  for (int i = 1; i <= E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    //		e[u].emplace_back(v,  i);
    //		e[v].emplace_back(u, -i);
    //		C[i] = c;
    fl.addEdge(u, v, c);
  }
  //	int ans = 0;
  //	while(1){
  //		bool bf[105] = {};
  //		int add = solve(0, INF, bf);
  //		ans += add;
  //		if( add == 0 ) break;
  //	}

  cout << fl.maxFlow(0, V - 1) << endl;

  return 0;
}