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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
struct Edge {
  int src;
  int dst;
  int rev;
  int cap;
};
typedef vector<vector<Edge>> Graph;
void add_edge(Graph &g, int s, int t, int c) {
  g[s].push_back(Edge{s, t, int(g[t].size()), c});
  g[t].push_back(Edge{t, s, int(g[s].size()) - 1, 0});
}
struct Compare {
public:
  bool operator()(const pair<int, int> &l, const pair<int, int> &r) {
    return l.first < r.first;
  }
};
const int AMAX = INT_MAX / 2;
int maximumflow(Graph &g, int start, int goal) {
  int ans = 0;
  while (true) {
    int flow = 0;
    priority_queue<pair<int, int>> que;
    que.emplace(AMAX, 0);
    vector<int> memo(g.size(), 0), rev(g.size(), -1);
    memo[start] = AMAX;
    while (!que.empty()) {
      auto atop(que.top());

      que.pop();
      const int now(atop.second);
      const int f(atop.first);
      if (now == goal) {
        flow = f;
        break;
      }
      for (auto e : g[now]) {
        const int nextcost(min(f, e.cap));
        if (memo[e.dst] < min(f, nextcost)) {
          memo[e.dst] = min(f, nextcost);
          rev[e.dst] = e.rev;
          que.emplace(nextcost, e.dst);
        }
      }
    }
    if (flow == 0)
      return ans;
    ans += flow;
    int now(goal);
    while (now != start) {
      g[now][rev[now]].cap += flow;
      int next(g[now][rev[now]].dst);
      g[next][g[now][rev[now]].rev].cap -= flow;
      now = next;
    }
    int ss;
    ss = 0;
    ss += 1;
    ans += ss;
    ans -= ss;
  }
}

int main() {
  int V, E;
  cin >> V >> E;
  Graph g(V);
  for (int i = 0; i < E; ++i) {
    int s, t, c;
    cin >> s >> t >> c;
    add_edge(g, s, t, c);
  }
  int ans = maximumflow(g, 0, V - 1);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
struct Edge {
  int src;
  int dst;
  int rev;
  int cap;
};
typedef vector<vector<Edge>> Graph;
void add_edge(Graph &g, int s, int t, int c) {
  g[s].push_back(Edge{s, t, int(g[t].size()), c});
  g[t].push_back(Edge{t, s, int(g[s].size()) - 1, 0});
}
struct Compare {
public:
  bool operator()(const pair<int, int> &l, const pair<int, int> &r) {
    return l.first < r.first;
  }
};
const int AMAX = INT_MAX / 2;
int maximumflow(Graph &g, int start, int goal) {
  int ans = 0;
  while (true) {
    int flow = 0;
    priority_queue<pair<int, int>> que;
    que.emplace(AMAX, 0);
    vector<int> memo(g.size(), 0), rev(g.size(), -1);
    memo[start] = AMAX;
    while (!que.empty()) {
      auto atop(que.top());

      que.pop();
      const int now(atop.second);
      const int f(atop.first);
      if (now == goal) {
        flow = f;
        break;
      }
      for (auto e : g[now]) {
        if (memo[e.dst] < min(f, e.cap)) {
          memo[e.dst] = min(f, e.cap);
          rev[e.dst] = e.rev;
          que.emplace(e.cap, e.dst);
        }
      }
    }
    if (flow == 0)
      return ans;
    ans += flow;
    int now(goal);
    while (now != start) {
      g[now][rev[now]].cap += flow;
      int next(g[now][rev[now]].dst);
      g[next][g[now][rev[now]].rev].cap -= flow;
      now = next;
    }
    int ss;
    ss = 0;
    ss += 1;
    ans += ss;
    ans -= ss;
  }
}

int main() {
  int V, E;
  cin >> V >> E;
  Graph g(V);
  for (int i = 0; i < E; ++i) {
    int s, t, c;
    cin >> s >> t >> c;
    add_edge(g, s, t, c);
  }
  int ans = maximumflow(g, 0, V - 1);
  cout << ans << endl;
  return 0;
}