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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int v;
int graph[101][101] = {};
int residual[101][101] = {};

pair<vector<int>, int> bfs() {
  vector<int> next(101, -1);
  queue<int> qu;
  qu.push(v - 1);
  while (!qu.empty()) {
    int now = qu.front();
    qu.pop();
    for (int i = 0; i < v - 1; i++) {
      if (residual[i][now] > 0 && next[i] == -1) {
        next[i] = now;
        qu.push(i);
      }
    }
  }
  int minc = mod;
  vector<int> ans;
  int now = 0;
  if (next[now] == -1)
    return make_pair(ans, minc);
  while (now != v - 1) {
    minc = min(minc, residual[now][next[now]]);
    ans.push_back(now);
    now = next[now];
  }
  ans.push_back(v - 1);
  return make_pair(ans, minc);
}

int main() {
  int e;
  cin >> v >> e;
  for (int i = 0; i < e; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    // graph[u][v] = c;
    residual[u][v] = c;
  }
  auto tmp = bfs();
  vector<int> tmpv = tmp.first;
  int c = tmp.second;
  while (!tmpv.empty()) {
    for (int i = 0; i < tmpv.size() - 1; i++) {
      int f = tmpv[i];
      int t = tmpv[i + 1];
      // cout << f << " ";
      graph[f][t] += c;
      residual[f][t] -= c;
      residual[t][f] += c;
    }
    // cout << tmpv[tmpv.size() - 1] << " " << c << endl;
    // for(int i = 0; i < v; i++){
    // 	for(int j = 0; j < v; j++){
    // 		cout << graph[i][j] << " ";
    // 	}
    // 	cout << endl;
    // }
    // cout << endl;
    tmp = bfs();
    tmpv = tmp.first;
    c = tmp.second;
  }
  int ans = 0;
  for (int i = 0; i < v - 1; i++) {
    ans += graph[i][v - 1];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int v;
int graph[101][101] = {};
int residual[101][101] = {};
vector<bool> isused(101, false);

pair<vector<int>, int> dfs(int t) {
  if (t == 0)
    return make_pair(vector<int>(1, 0), mod);
  isused[t] = true;
  for (int i = 0; i < v; i++) {
    if (residual[i][t] == 0 || isused[i])
      continue;
    auto tmp = dfs(i);
    vector<int> tmpv = tmp.first;
    int minc = tmp.second;
    if (tmpv.empty())
      continue;
    tmpv.push_back(t);
    isused[t] = false;
    return make_pair(tmpv, min(minc, residual[i][t]));
  }
  isused[t] = false;
  return make_pair(vector<int>(), mod);
}

int main() {
  int e;
  cin >> v >> e;
  for (int i = 0; i < e; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    // graph[u][v] = c;
    residual[u][v] = c;
  }
  auto tmp = dfs(v - 1);
  vector<int> tmpv = tmp.first;
  int c = tmp.second;
  while (!tmpv.empty()) {
    for (int i = 0; i < tmpv.size() - 1; i++) {
      int f = tmpv[i];
      int t = tmpv[i + 1];
      // cout << f << " ";
      graph[f][t] += c;
      residual[f][t] -= c;
      residual[t][f] += c;
    }
    // cout << tmpv[tmpv.size() - 1] << " " << c << endl;
    // for(int i = 0; i < v; i++){
    // 	for(int j = 0; j < v; j++){
    // 		cout << graph[i][j] << " ";
    // 	}
    // 	cout << endl;
    // }
    // cout << endl;
    tmp = dfs(v - 1);
    tmpv = tmp.first;
    c = tmp.second;
  }
  int ans = 0;
  for (int i = 0; i < v - 1; i++) {
    ans += graph[i][v - 1];
  }
  cout << ans << endl;
}