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

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int inf = 1LL << 55;

struct edge {
  int to, cap, rev, ini;

  edge() {}

  edge(int to, int cap, int rev, int ini)
      : to(to), cap(cap), rev(rev), ini(ini) {}
};

using Pi = pair<int, int>;

struct Dinic {
  vector<vector<edge>> graph;
  vector<int> iter, level;

  Dinic(int V) : graph(V), iter(V), level(V) {}

  Pi add_edge(int from, int to, int cap) {
    graph[from].emplace_back(to, cap, graph[to].size(), cap);
    graph[to].emplace_back(from, cap, graph[from].size() - 1, 0);
    return Pi(from, graph[from].size() - 1);
  }

  bool bfs(int s, int t) {
    queue<int> que;
    fill(level.begin(), level.end(), -1);
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (edge e : graph[v]) {
        if (e.cap > 0 && level[e.to] < 0) {
          que.push(e.to);
          level[e.to] = level[v] + 1;
        }
      }
    }
    return level[t] >= 0;
  }

  int dfs(int v, int t, int f) {
    if (v == t)
      return f;
    for (int &i = iter[v]; i < (int)graph[v].size(); i++) {
      edge &e = graph[v][i];
      if (e.cap > 0 && level[e.to] > level[v]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  int max_flow(int s, int t, int ff = inf) {
    int flow = 0;
    while (bfs(s, t) && flow < ff) {
      fill(iter.begin(), iter.end(), 0);
      int f;
      while ((f = dfs(s, t, ff)) > 0 && flow < ff)
        flow += f;
    }
    return flow;
  }
};

signed main() {
  int V, E;
  cin >> V >> E;
  Dinic graph(V);
  vector<Pi> vec;
  for (int i = 0; i < E; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    Pi p = graph.add_edge(s, t, c);
    if (c == 1)
      vec.push_back(p);
  }
  int flow = graph.max_flow(0, V - 1);
  int flag = 0;
  for (Pi p : vec) {
    edge &e = graph.graph[p.first][p.second];
    if (e.cap == 0 || graph.graph[e.to][e.rev].cap == 0) {
      bool hoge = e.cap == 0;
      e.cap = graph.graph[e.to][e.rev].cap = 0;
      if (hoge) {
        if (e.to != V - 1)
          graph.max_flow(V - 1, e.to, 1);
        if (p.first != 0)
          graph.max_flow(p.first, 0, 1);
      } else {
        if (p.first != V - 1)
          graph.max_flow(V - 1, p.first, 1);
        if (e.to != 0)
          graph.max_flow(e.to, 0, 1);
      }
      int f = graph.max_flow(0, V - 1, 1);
      if (f == 0) {
        flag = 1;
        break;
      }

      if (hoge) {
        graph.graph[e.to][e.rev].cap = 1;
        if (e.to != V - 1)
          graph.max_flow(e.to, V - 1, 1);
        if (p.first != 0)
          graph.max_flow(0, p.first, 1);
      } else {
        e.cap = 1;
        if (p.first != V - 1)
          graph.max_flow(p.first, V - 1, 1);
        if (e.to != 0)
          graph.max_flow(0, e.to, 1);
      }
    }
  }
  if (flow - flag > 10000)
    cout << -1 << endl;
  else
    cout << flow - flag << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int inf = 1LL << 55;

struct edge {
  int to, cap, rev, ini;
  edge() {}
  edge(int to, int cap, int rev, int ini)
      : to(to), cap(cap), rev(rev), ini(ini) {}
};

using Pi = pair<int, int>;

struct Dinic {
  vector<vector<edge>> graph;
  vector<int> iter, level;
  Dinic(int V) : graph(V), iter(V), level(V) {}
  Pi add_edge(int from, int to, int cap) {
    graph[from].emplace_back(to, cap, graph[to].size(), cap);
    graph[to].emplace_back(from, 0, graph[from].size() - 1, 0);
    return Pi(from, graph[from].size() - 1);
  }
  bool bfs(int s, int t) {
    queue<int> que;
    fill(level.begin(), level.end(), -1);
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (edge e : graph[v]) {
        if (e.cap > 0 && level[e.to] < 0) {
          que.push(e.to);
          level[e.to] = level[v] + 1;
        }
      }
    }
    return level[t] >= 0;
  }
  int dfs(int v, int t, int f) {
    if (v == t)
      return f;
    for (int &i = iter[v]; i < (int)graph[v].size(); i++) {
      edge &e = graph[v][i];
      // cout << v <<" "<<t << " " << e.to << " " << level[e.to] << " " <<
      // level[v] << endl;
      if (e.cap > 0 && level[e.to] > level[v]) {
        int d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }
  int max_flow(int s, int t, int ff = inf) {
    int flow = 0;
    // cout << s << " " << t << endl;
    while (bfs(s, t) && flow < ff) {
      // cout << "LEVEL " << t << " " << level[t] << endl;
      fill(iter.begin(), iter.end(), 0);
      int f;
      while ((f = dfs(s, t, ff)) > 0 && flow < ff)
        flow += f;
    }
    return flow;
  }
};

signed main() {
  int V, E;
  cin >> V >> E;
  Dinic graph(V);
  // vector<Pi> vec;
  vector<int> vec;
  vector<tuple<int, int, int>> es;
  for (int i = 0; i < E; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    es.emplace_back(s, t, c);
    Pi p = graph.add_edge(s, t, c);
    Pi q = graph.add_edge(t, s, c);
    if (c == 1)
      vec.push_back(i); // vec.push_back(p);
  }
  int flow = graph.max_flow(0, V - 1);
  int flag = 0;
  /*
  for(Pi p : vec) {
    edge& e = graph.graph[p.first][p.second];
    if(e.cap == 0) {
      graph.graph[e.to][e.rev].cap  = 0;
      if(e.to != V-1) graph.max_flow(V-1, e.to, 1);
      if(p.first != 0) graph.max_flow(p.first, 0, 1);
      int f = graph.max_flow(0, V-1, 1);
      if(f == 0) {
        flag = 1;
        break;
      }
    }
  }
  */
  for (int i : vec) {
    Dinic G(V);
    for (int j = 0; j < E; j++) {
      if (i == j)
        continue;
      int s, t, c;
      tie(s, t, c) = es[j];
      G.add_edge(s, t, c);
      G.add_edge(t, s, c);
    }
    int ff = G.max_flow(0, V - 1, flow);
    if (ff == flow - 1) {
      flag = 1;
      break;
    }
  }
  if (flow - flag > 10000)
    cout << -1 << endl;
  else
    cout << flow - flag << endl;

  return 0;
}