Problem p02376 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct Edge {
  int from, to, flow, rev;
  Edge() {}
  Edge(int from, int to, int flow, int rev) {
    this->from = from;
    this->to = to;
    this->flow = flow;
    this->rev = rev;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

const int VMAX = 100;

void add_edge(Graph &g, int s, int t, int flow) {
  g[s].push_back(Edge(s, t, flow, g[t].size()));
  g[t].push_back(Edge(t, s, 0, g[s].size() - 1));
}

int dfs(Graph &g, int s, int t, int level[], int seenEdgeCount[], int flow) {
  if (s == t)
    return flow;

  for (int &i = seenEdgeCount[s]; i < g[s].size(); i++) {
    int nv = g[s][i].to;
    if (level[nv] != level[s] + 1 || g[s][i].flow <= 0)
      continue; // 辺残量も確認する！
    int f = dfs(g, nv, t, level, seenEdgeCount, min(flow, g[s][i].flow));
    if (f <= 0)
      continue;
    // s --> nv間にfだけ流す
    g[s][i].flow -= f;
    g[nv][g[s][i].rev].flow += f;
    return f;
  }
  return 0;
}

int maxflow(Graph g, int s, int t) {
  int i, level[VMAX], ret = 0;

  while (true) {
    for (i = 0; i < g.size(); i++)
      level[i] = -1;
    queue<int> que;

    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (i = 0; i < g[v].size(); i++) {
        if (g[v][i].flow <= 0)
          continue;
        int nv = g[v][i].to;
        if (level[nv] == -1) {
          level[nv] = level[v] + 1;
          que.push(nv);
        }
      }
    }
    if (level[t] == -1)
      break;

    int seenEdgeCount[VMAX] = {0};

    int flowINF = 1e+9; // 辺容量の最大値よりも大きな値にする

    while (true) {
      int f = dfs(g, s, t, level, seenEdgeCount, flowINF);
      if (f <= 0)
        break;
      ret += f;
    }
  }
  return ret;
}

int n, m;
Graph g;

int main() {
  int i;

  cin >> n >> m;
  g.resize(n);
  for (i = 0; i < m; i++) {
    int u, v, f;
    cin >> u >> v >> f;
    add_edge(g, u, v, f);
  }
  cout << maxflow(g, 0, n - 1) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct Edge {
  int from, to, flow, rev;
  Edge() {}
  Edge(int from, int to, int flow, int rev) {
    this->from = from;
    this->to = to;
    this->flow = flow;
    this->rev = rev;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

const int VMAX = 100;

void add_edge(Graph &g, int s, int t, int flow) {
  g[s].push_back(Edge(s, t, flow, g[t].size()));
  g[t].push_back(Edge(t, s, 0, g[s].size() - 1));
}

int dfs(Graph &g, int s, int t, int level[], int seenEdgeCount[], int flow) {
  if (s == t)
    return flow;

  for (int &i = seenEdgeCount[s]; i < g[s].size(); i++) {
    int nv = g[s][i].to;
    if (level[nv] != level[s] + 1)
      continue;
    int f = dfs(g, nv, t, level, seenEdgeCount, min(flow, g[s][i].flow));
    if (f <= 0)
      continue;
    // s --> nv間にfだけ流す
    g[s][i].flow -= f;
    g[nv][g[s][i].rev].flow += f;
    return f;
  }
  return 0;
}

int maxflow(Graph g, int s, int t) {
  int i, level[VMAX], ret = 0;

  while (true) {
    for (i = 0; i < VMAX; i++)
      level[i] = -1;
    queue<int> que;

    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (i = 0; i < g[v].size(); i++) {
        if (g[v][i].flow <= 0)
          continue;
        int nv = g[v][i].to;
        if (level[nv] == -1) {
          level[nv] = level[v] + 1;
          que.push(nv);
        }
      }
    }
    if (level[t] == -1)
      break;

    int seenEdgeCount[VMAX] = {0};
    int flowINF = 1e+9; // 辺容量の最大値よりも大きな値にする

    while (true) {
      int f = dfs(g, s, t, level, seenEdgeCount, flowINF);
      if (f <= 0)
        break;
      ret += f;
    }
  }
  return ret;
}

int n, m;
Graph g;

int main() {
  int i;

  cin >> n >> m;
  g.resize(n);
  for (i = 0; i < m; i++) {
    int u, v, f;
    cin >> u >> v >> f;
    add_edge(g, u, v, f);
  }
  cout << maxflow(g, 0, n - 1) << endl;
  return 0;
}