Problem p01459 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define chmin(a, b) (a) = min((a), (b));
#define chmax(a, b) (a) = max((a), (b));
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(v)                                                              \\
  cerr << (#v) << ":";                                                         \\
  for (auto(x) : (v)) {                                                        \\
    cerr << " " << (x);                                                        \\
  }                                                                            \\
  cerr << endl;
#define printVS(vs)                                                            \\
  cerr << (#vs) << ":" << endl;                                                \\
  for (auto(s) : (vs)) {                                                       \\
    cerr << (s) << endl;                                                       \\
  }
#define printVV(vv)                                                            \\
  cerr << (#vv) << ":" << endl;                                                \\
  for (auto(v) : (vv)) {                                                       \\
    for (auto(x) : (v)) {                                                      \\
      cerr << " " << (x);                                                      \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define printP(p) cerr << (#p) << (p).first << " " << (p).second << endl;
#define printVP(vp)                                                            \\
  cerr << (#vp) << ":" << endl;                                                \\
  for (auto(p) : (vp)) {                                                       \\
    cerr << (p).first << " " << (p).second << endl;                            \\
  }

inline void output() { cerr << endl; }
template <typename First, typename... Rest>
inline void output(const First &first, const Rest &...rest) {
  cerr << first << " ";
  output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
// using Graph = vector<vector<int>>;

int H, W, A;
vector<string> s(H);
int sx, sy, gx, gy;
const int MAX_V = 4 * 11 * 11 * 100 * 100;
int d[MAX_V];

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

bool inside(int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; }

struct Node {
  int x, y, p, q, k, id;
  Node() {}
  Node(int _x, int _y, int _p, int _q, int _k)
      : x(_x), y(_y), p(_p), q(_q), k(_k), id(-1) {
    if (!inside(x, y))
      return;
    if (p < 0 || q < 0)
      return;
    if (s[x][y] == '#')
      return;
    if (s[x][y] == 'S' && k == 2)
      return;
    id = 4 * 11 * 11 * 100 * x + 4 * 11 * 11 * y + 4 * 11 * p + 4 * q + k;
  }
  Node(int _id) : id(_id) {
    k = _id % 4;
    _id /= 4;
    q = _id % 11;
    _id /= 11;
    p = _id % 11;
    _id /= 11;
    y = _id % 100;
    _id /= 100;
    x = _id;
    // output(x, y, p, q, k, id);
    assert(id ==
           4 * 11 * 11 * 100 * x + 4 * 11 * 11 * y + 4 * 11 * p + 4 * q + k);
  }
  void print() { output(x, y, p, q, k, id); }
};

void dijkstra() {
  using State = pair<int, int>;
  priority_queue<State, vector<State>, greater<State>> pq; // cost, vertex
  fill(d, d + MAX_V, inf);

  Node ns(sx, sy, A, A, 0);
  d[ns.id] = 0;
  pq.push(State(0, ns.id));

  while (!pq.empty()) {
    int cost, id;
    tie(cost, id) = pq.top();
    pq.pop();
    if (d[id] < cost)
      continue;
    Node u(id);

    // output("---");
    // cout << cost << " ";
    // u.print();

    int x = u.x;
    int y = u.y;
    int p = u.p;
    int q = u.q;
    int k = u.k;

    vector<pair<Node, int>> cands; // v, cost(u->v)

    if (s[x][y] == 'S') {
      cands.emplace_back(Node(x + dx[k], y + dy[k], p, q, k), 0);
    } else if (k == 0) {
      cands.emplace_back(Node(x, y - 1, p, q - 1, 3), 1);
      cands.emplace_back(Node(x, y + 1, p - 1, q, 1), 1);
      cands.emplace_back(Node(x + 1, y, p, q, 0), 0);
    } else if (k == 1) {
      cands.emplace_back(Node(x - 1, y, p, q - 1, 2), 1);
      cands.emplace_back(Node(x + 1, y, p - 1, q, 0), 1);
      cands.emplace_back(Node(x, y + 1, p, q, 1), 0);
    } else if (k == 2) {
      cands.emplace_back(Node(x, y - 1, p - 1, q, 3), 1);
      cands.emplace_back(Node(x, y + 1, p, q - 1, 1), 1);
      cands.emplace_back(Node(x - 1, y, p, q, 2), 0);
    } else if (k == 3) {
      cands.emplace_back(Node(x - 1, y, p - 1, q, 2), 1);
      cands.emplace_back(Node(x + 1, y, p, q - 1, 0), 1);
      cands.emplace_back(Node(x, y - 1, p, q, 3), 0);
    }

    for (const auto &p : cands) {
      Node v;
      int c;
      tie(v, c) = p;
      if (v.id == -1)
        continue;

      // cout << c << " ";
      // v.print();

      if (d[v.id] > d[u.id] + c) {
        d[v.id] = d[u.id] + c;
        pq.push(State(d[v.id], v.id));
      }
    }

    // output("---");
  }
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  cin >> H >> W >> A;
  s.resize(H);
  rep(i, H) {
    cin >> s[i];
    rep(j, W) {
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (s[i][j] == 'G') {
        gx = i;
        gy = j;
      }
    }
  }

  dijkstra();

  int mi = inf;
  rep(p, A + 1) {
    rep(q, A + 1) {
      rep(k, 4) {
        Node u(gx, gy, p, q, k);
        // cout << d[u.id] << " ";
        // u.print();
        if (u.id == -1)
          continue;
        mi = min(mi, d[u.id]);
      }
    }
  }

  cout << (mi == inf ? -1 : mi) << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define chmin(a, b) (a) = min((a), (b));
#define chmax(a, b) (a) = max((a), (b));
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(v)                                                              \\
  cerr << (#v) << ":";                                                         \\
  for (auto(x) : (v)) {                                                        \\
    cerr << " " << (x);                                                        \\
  }                                                                            \\
  cerr << endl;
#define printVS(vs)                                                            \\
  cerr << (#vs) << ":" << endl;                                                \\
  for (auto(s) : (vs)) {                                                       \\
    cerr << (s) << endl;                                                       \\
  }
#define printVV(vv)                                                            \\
  cerr << (#vv) << ":" << endl;                                                \\
  for (auto(v) : (vv)) {                                                       \\
    for (auto(x) : (v)) {                                                      \\
      cerr << " " << (x);                                                      \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define printP(p) cerr << (#p) << (p).first << " " << (p).second << endl;
#define printVP(vp)                                                            \\
  cerr << (#vp) << ":" << endl;                                                \\
  for (auto(p) : (vp)) {                                                       \\
    cerr << (p).first << " " << (p).second << endl;                            \\
  }

inline void output() { cerr << endl; }
template <typename First, typename... Rest>
inline void output(const First &first, const Rest &...rest) {
  cerr << first << " ";
  output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
// using Graph = vector<vector<int>>;

int H, W, A;
vector<string> s(H);
int sx, sy, gx, gy;
const int MAX_V = 4 * 11 * 11 * 100 * 100;

bool inside(int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; }

struct Node {
  int x, y, p, q, k, id;
  Node() {}
  Node(int _x, int _y, int _p, int _q, int _k)
      : x(_x), y(_y), p(_p), q(_q), k(_k), id(-1) {
    if (!inside(x, y))
      return;
    if (p < 0 || q < 0)
      return;
    if (s[x][y] == '#')
      return;
    if (s[x][y] == 'S' && k == 2)
      return;
    id = 4 * 11 * 11 * 100 * x + 4 * 11 * 11 * y + 4 * 11 * p + 4 * q + k;
  }
  Node(int _id) : id(_id) {
    k = _id % 4;
    _id /= 4;
    q = _id % 11;
    _id /= 11;
    p = _id % 11;
    _id /= 11;
    y = _id % 100;
    _id /= 100;
    x = _id;
    // output(x, y, p, q, k, id);
    assert(id ==
           4 * 11 * 11 * 100 * x + 4 * 11 * 11 * y + 4 * 11 * p + 4 * q + k);
  }
  void print() { output(x, y, p, q, k, id); }
};

struct edge {
  int to, cost;
  edge() {}
  edge(int _to, int _cost) : to(_to), cost(_cost) {}
};
vector<edge> G[MAX_V];
int d[MAX_V];

// directed
void addEdge(Node u, Node v, int c) {
  if (u.id == -1)
    return;
  if (v.id == -1)
    return;
  G[u.id].emplace_back(v.id, c);
  // u.print();
  // v.print();
  // output(c);
}
void dijkstra() {
  using State = pair<int, int>;
  priority_queue<State, vector<State>, greater<State>> pq; // cost, vertex
  fill(d, d + MAX_V, inf);

  Node ns(sx, sy, A, A, 0);
  d[ns.id] = 0;
  pq.push(State(0, ns.id));

  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();

    int cost = p.first;
    int id = p.second;
    Node u(id);

    // output("---");
    // cout << cost << " ";
    // u.print();

    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (const auto &e : G[v]) {
      //
      // Node x(e.to);
      // cout << e.cost << " ";
      // x.print();

      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(State(d[e.to], e.to));
      }
    }

    // output("---");
  }
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  cin >> H >> W >> A;
  s.resize(H);
  rep(i, H) {
    cin >> s[i];
    rep(j, W) {
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (s[i][j] == 'G') {
        gx = i;
        gy = j;
      }
    }
  }

  rep(x, H) {
    rep(y, W) {
      if (s[x][y] == '#')
        continue;
      rep(p, A + 1) {
        rep(q, A + 1) {
          rep(k, 4) {
            Node u(x, y, p, q, k);
            // u.print();
            if (k == 0) {
              addEdge(u, Node(x, y - 1, p, q - 1, 3), 1);
              addEdge(u, Node(x, y + 1, p - 1, q, 1), 1);
              addEdge(u, Node(x + 1, y, p, q, 0), 0);
            } else if (k == 1) {
              addEdge(u, Node(x - 1, y, p, q - 1, 2), 1);
              addEdge(u, Node(x + 1, y, p - 1, q, 0), 1);
              addEdge(u, Node(x, y + 1, p, q, 1), 0);
            } else if (k == 2) {
              addEdge(u, Node(x, y - 1, p - 1, q, 3), 1);
              addEdge(u, Node(x, y + 1, p, q - 1, 1), 1);
              addEdge(u, Node(x - 1, y, p, q, 2), 0);
            } else if (k == 3) {
              addEdge(u, Node(x - 1, y, p - 1, q, 2), 1);
              addEdge(u, Node(x + 1, y, p, q - 1, 0), 1);
              addEdge(u, Node(x, y - 1, p, q, 3), 0);
            }
          }
        }
      }
    }
  }

  // rep(id, MAX_V) {
  //     Node u(id);
  //     output("---");
  //     u.print();
  //     for (auto e : G[id]) {
  //         Node v(e.to);
  //         v.print();
  //         cout << e.cost << endl;
  //     }
  //     output("---");
  // }

  // cerr << "hey!" << endl;

  dijkstra();

  // cerr << "hey!" << endl;

  int mi = inf;
  rep(p, A + 1) {
    rep(q, A + 1) {
      rep(k, 4) {
        Node u(gx, gy, p, q, k);
        // cout << d[u.id] << " ";
        // u.print();
        if (u.id == -1)
          continue;
        mi = min(mi, d[u.id]);
      }
    }
  }

  cout << (mi == inf ? -1 : mi) << endl;

  // rep(x, H) {
  //     rep(y, W) {
  //         rep(p, A + 1) {
  //             rep(q, A + 1) {
  //                 rep(k, 4) {
  //                     Node u(x, y, p, q, k);
  //                     output(x, y, p, q, k, ":", d[u.id]);
  //                 }
  //             }
  //         }
  //     }
  // }
}