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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll, ll> pint;

const int INF = 1000100010;
const int MAX_N = 102;
struct edge {
  int to, cap, rev;
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

vector<edge> g[MAX_N];
int dist[MAX_N];
int used[MAX_N];
void add_edge(int src, int dst, int cap) {
  g[src].pb(edge(dst, cap, g[dst].size()));
  g[dst].pb(edge(src, cap, g[src].size() - 1));
}
void bfs(int s) {
  memset(dist, -1, sizeof(dist));
  queue<int> q;
  dist[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    rep(i, g[v].size()) {
      edge &e = g[v][i];
      if (e.cap > 0 && dist[e.to] < 0) {
        dist[e.to] = dist[v] + 1;
        q.push(e.to);
      }
    }
  }
}
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  rep(i, g[v].size()) {
    edge &e = g[v][i];
    if (e.cap > 0 && dist[v] < dist[e.to]) {
      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) {
  int flow = 0;
  while (1) {
    bfs(s);
    if (dist[t] < 0)
      return flow;
    memset(used, 0, sizeof(used));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}
vector<edge> g2[MAX_N];
int main() {
  int k, n, m;
  int a, b, c;
  cin >> k >> n >> m;
  bool flag = false;
  rep(i, m) {
    cin >> a >> b >> c;
    add_edge(a, b, c);
    if (a == 0 && b <= k || b == 0 && a <= k)
      flag = true;
  }
  if (flag) {
    cout << "overfuro" << endl;
    return 0;
  }
  int s = n + k + 1;
  FOR(i, 1, k + 1) { add_edge(s, i, INF); }
  int cur = max_flow(s, 0);
  // cout<<cur<<endl;
  int mx = 0;
  rep(i, s + 1) g2[i] = g[i];
  rep(i, s + 1) {
    rep(j, g[i].size()) {
      if (g[i][j].cap == 0) {
        g[i][j].cap = INF;
        mx = max(mx, max_flow(s, 0));
        rep(l, s + 1) g[l] = g2[l];
      }
    }
  }
  if (cur + mx >= INF)
    cout << "overfuro" << endl;
  else
    cout << cur + mx << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll, ll> pint;

const int INF = 1000100010;
const int MAX_N = 102;
struct edge {
  int to, cap, rev;
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

vector<edge> g[MAX_N];
bool used[MAX_N];
void add_edge(int src, int dst, int cap) {
  g[src].pb(edge(dst, cap, g[dst].size()));
  g[dst].pb(edge(src, cap, g[src].size() - 1));
}
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  rep(i, g[v].size()) {
    edge &e = g[v][i];
    if (!used[e.to] && e.cap > 0) {
      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) {
  int flow = 0;
  while (1) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}
vector<edge> g2[MAX_N];
int main() {
  int k, n, m;
  int a, b, c;
  cin >> k >> n >> m;
  bool flag = false;
  rep(i, m) {
    cin >> a >> b >> c;
    add_edge(a, b, c);
    if (a == 0 && b <= k || b == 0 && a <= k)
      flag = true;
  }
  if (flag) {
    cout << "overfuro" << endl;
    return 0;
  }
  int s = n + k + 1;
  FOR(i, 1, k + 1) { add_edge(s, i, INF); }
  int cur = max_flow(s, 0);
  // cout<<cur<<endl;
  int mx = 0;
  rep(i, s + 1) g2[i] = g[i];
  rep(i, s + 1) {
    rep(j, g[i].size()) {
      if (g[i][j].cap == 0) {
        g[i][j].cap = INF;
        mx = max(mx, max_flow(s, 0));
        rep(i, s + 1) g[i] = g2[i];
      }
    }
  }
  if (cur + mx >= INF)
    cout << "overfuro" << endl;
  else
    cout << cur + mx << endl;
  return 0;
}