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

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

using Flow = int;
using Cost = int;
class PrimalDual {
public:
  struct Edge {
    int d;
    Flow c, f;
    Cost w;
    int r;
    Edge(int d, Flow c, Flow f, Cost w, int r) : d(d), c(c), f(f), w(w), r(r) {}
  };

  int n;
  vector<vector<Edge>> g;
  PrimalDual(int n) : n(n), g(n) {}

  void addEdge(int src, int dst, Flow cap, Cost cost) { // 有向辺
    int rsrc = g[dst].size();
    int rdst = g[src].size();
    g[src].emplace_back(dst, cap, 0, cost, rsrc);
    g[dst].emplace_back(src, cap, cap, -cost, rdst);
  }

  template <Cost inf = numeric_limits<Cost>::max() / 8>
  Cost solve(int s, int t, Flow f) {
    Cost res = 0;
    vector<Cost> h(n), dist(n);
    vector<int> prevv(n), preve(n);

    using state = pair<Cost, int>;
    priority_queue<state, vector<state>, greater<state>> q;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      fill(dist.begin(), dist.end(), inf);
      dist[s] = 0;
      q.emplace(0, s);
      while (q.size()) {
        Cost cd;
        int v;
        tie(cd, v) = q.top();
        q.pop();
        if (dist[v] < cd)
          continue;
        for (int i = 0; i < g[v].size(); ++i) {
          const Edge &e = g[v][i];
          if (residue(e) == 0)
            continue;
          if (dist[e.d] + h[e.d] > cd + h[v] + e.w) {
            dist[e.d] = dist[v] + e.w + h[v] - h[e.d];
            prevv[e.d] = v;
            preve[e.d] = i;
            q.emplace(dist[e.d], e.d);
          }
        }
      }

      if (dist[t] == inf)
        return -1;

      for (int i = 0; i < n; ++i)
        h[i] += dist[i];
      Flow d = f;
      for (int v = t; v != s; v = prevv[v])
        cmin(d, residue(g[prevv[v]][preve[v]]));
      f -= d;
      res += d * h[t];
      for (int v = t; v != s; v = prevv[v]) {
        Edge &e = g[prevv[v]][preve[v]];
        e.f += d;
        g[v][e.r].f -= d;
      }
    }
    return res;
  }

  Flow residue(const Edge &e) { return e.c - e.f; }
};

main {
  int v, e, f;
  cin >> v >> e >> f;
  PrimalDual pd(v);
  rep(i, e) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    pd.addEdge(a, b, c, d);
  }
  cout << pd.solve(0, v - 1, f) << endl;
}


Error: TLE

Fixed submission:

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

using Flow = int;
using Cost = int;
class PrimalDual {
public:
  struct Edge {
    int d;
    Flow c, f;
    Cost w;
    int r;
    bool is_r;
    Edge(int d, Flow c, Flow f, Cost w, int r, bool is_r)
        : d(d), c(c), f(f), w(w), r(r), is_r(is_r) {}
  };

  int n;
  vector<vector<Edge>> g;
  PrimalDual(int n) : n(n), g(n) {}

  void addEdge(int src, int dst, Flow cap, Cost cost) { // 有向辺
    int rsrc = g[dst].size();
    int rdst = g[src].size();
    g[src].emplace_back(dst, cap, 0, cost, rsrc, false);
    g[dst].emplace_back(src, cap, cap, -cost, rdst, true);
  }

  template <Cost inf = numeric_limits<Cost>::max() / 8>
  Cost solve(int s, int t, Flow f) {
    Cost res = 0;
    vector<Cost> h(n), dist(n);
    vector<int> prevv(n), preve(n);

    using state = pair<Cost, int>;
    priority_queue<state, vector<state>, greater<state>> q;
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      fill(dist.begin(), dist.end(), inf);
      dist[s] = 0;
      q.emplace(0, s);
      while (q.size()) {
        Cost cd;
        int v;
        tie(cd, v) = q.top();
        q.pop();
        if (dist[v] < cd)
          continue;
        for (int i = 0; i < g[v].size(); ++i) {
          const Edge &e = g[v][i];
          if (residue(e) == 0)
            continue;
          if (dist[e.d] + h[e.d] > cd + h[v] + e.w) {
            dist[e.d] = dist[v] + e.w + h[v] - h[e.d];
            prevv[e.d] = v;
            preve[e.d] = i;
            q.emplace(dist[e.d], e.d);
          }
        }
      }

      if (dist[t] == INF)
        return -1;

      for (int i = 0; i < n; ++i)
        h[i] += dist[i];
      Flow d = f;
      for (int v = t; v != s; v = prevv[v])
        cmin(d, residue(g[prevv[v]][preve[v]]));
      f -= d;
      res += d * h[t];
      for (int v = t; v != s; v = prevv[v]) {
        Edge &e = g[prevv[v]][preve[v]];
        e.f += d;
        g[v][e.r].f -= d;
      }
    }
    return res;
  }

  Flow residue(const Edge &e) { return e.c - e.f; }
};

main {
  int v, e, f;
  cin >> v >> e >> f;
  PrimalDual pd(v);
  rep(i, e) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    pd.addEdge(a, b, c, d);
  }
  cout << pd.solve(0, v - 1, f) << endl;
}