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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

const pii Dir[8] = { //?????????
    {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

signed main() {
  int w, h, k;
  scanf("%d %d %d", &w, &h, &k);
  vector<vector<pii>> sww(w + 1), swh(h + 1), graph(k + 2);
  int spos = k, gpos = k + 1;
  bool als = 0, alg = 0;
  REP(i, k) {
    int a, b;
    scanf("%d %d", &a, &b);
    sww[a].push_back({b, i});
    swh[b].push_back({a, i});
    if (a == 1 && b == 1) {
      als = 1;
      spos = i;
    }
    if (a == w && b == h) {
      alg = 1;
      gpos = i;
    }
  }
  if (!als) {
    sww[1].push_back({1, k});
    swh[1].push_back({1, k});
  }
  if (!alg) {
    sww[w].push_back({h, k + 1});
    swh[h].push_back({w, k + 1});
  }
  REP(i, w + 1)
  sort(ALL(sww[i]));
  REP(i, h + 1)
  sort(ALL(swh[i]));

  REP(i, w + 1) {
    for (int j = 0; j < (int)sww[i].size() - 1; j++) {
      int dis = abs(sww[i][j].first - sww[i][j + 1].first);
      graph[sww[i][j].second].push_back({sww[i][j + 1].second, dis});
      graph[sww[i][j + 1].second].push_back({sww[i][j].second, dis});
    }
  }
  REP(i, h + 1) {
    for (int j = 0; (int)swh[i].size() - 1 > j; j++) {
      if (i == 1 && sww[i][j].first == 1 && !als)
        continue;
      int dis = abs(swh[i][j].first - swh[i][j + 1].first);
      graph[swh[i][j].second].push_back({swh[i][j + 1].second, -dis});
      graph[swh[i][j + 1].second].push_back({swh[i][j].second, -dis});
    }
  }

  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>>
      que;
  que.push({0, {spos, 0}});

  map<int, ll> al;

  while (que.size()) {
    auto top = que.top();
    ll cos = top.first;
    int pos = top.second.first, dir = top.second.second;
    que.pop();
    if (al.count(pos)) {
      continue;
    }
    al[pos] = cos;

    REP(i, (int)graph[pos].size()) {
      bool howcon = graph[pos][i].second < 0; //????????????????????????
      que.push({cos + abs(graph[pos][i].second) + (howcon != dir),
                {graph[pos][i].first, howcon}});
    }
  }
  if (!al.count(gpos))
    al[gpos] = -1;
  printf("%lld\\n", al[gpos]);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

const pii Dir[8] = { //?????????
    {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

signed main() {
  int w, h, k;
  scanf("%d %d %d", &w, &h, &k);
  vector<vector<int>> sww(w + 1), swh(h + 1);
  REP(i, k) {
    int a, b;
    scanf("%d %d", &a, &b);

    sww[a].push_back(b);
    swh[b].push_back(a);
  }
  sww[w].push_back(h);
  swh[h].push_back(w);
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>>
      que;
  map<pii, ll> time;
  que.push({0, {1, 1}});
  while (que.size()) {
    int curt = que.top().first;
    pii cur = que.top().second;
    que.pop();
    if (time.count(cur))
      continue;
    time[cur] = curt;
    if (cur == make_pair(w, h))
      break;
    REP(i, swh[cur.second].size()) {
      pii next = {swh[cur.second][i], cur.second};
      ll nt = 1 + curt +
              (abs(next.first - cur.first) + abs(next.second - cur.second));
      if (time.count(next))
        continue;
      que.push({nt, next});
    }
    REP(i, sww[cur.first].size()) {
      pii next = {cur.first, sww[cur.first][i]};
      ll nt = 1 + curt +
              (abs(next.first - cur.first) + abs(next.second - cur.second));
      if (time.count(next))
        continue;
      que.push({nt, next});
    }
  }
  if (time.count({w, h}))
    printf("%lld\\n", time[{w, h}]);
  else
    printf("-1\\n");
  return 0;
}