Problem p00528 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

#define int long long
#define PB push_back
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;

constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MAX_K = 100005;

int d[MAX_K][2], n, m, k;
vector<ppiii> xy, yx;
vector<pipii> G[MAX_K][2];

int Dijkstra() {
  fill(d[0], d[MAX_K], INF);
  d[k][0] = 0;
  priority_queue<pipii, vector<pipii>, greater<pipii>> pq;
  pq.push({0, {k, 0}});

  while (pq.size()) {
    pipii p = pq.top();
    pq.pop();
    int v = p.second.first;
    int st = p.second.second;
    if (d[v][st] < p.first)
      continue;

    for (int i = 0; i < G[v][st].size(); ++i) {
      int tov = G[v][st][i].second.first, tost = G[v][st][i].second.second;
      if (d[tov][tost] <= p.first + G[v][st][i].first)
        continue;
      d[tov][tost] = p.first + G[v][st][i].first;
      pq.push({d[tov][tost], {tov, tost}});
    }
  }

  int res = min(d[k + 1][0], d[k + 1][1]);
  return INF <= res ? -1 : res;
}

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

  cin >> m >> n >> k;

  for (int i = 0; i < k; ++i) {
    int x, y;
    cin >> x >> y;
    xy.PB({{x, y}, i});
    yx.PB({{y, x}, i});
    G[i][0].PB({1, {i, 1}});
    G[i][1].PB({1, {i, 0}});

    if (x == 1) {
      G[k][0].PB({y - 1, {i, 0}});
    }
    if (x == m) {
      G[i][0].PB({n - y, {k + 1, 0}});
    }
    if (y == n) {
      G[i][1].PB({m - x, {k + 1, 1}});
    }
  }
  sort(ALL(xy));
  sort(ALL(yx));

  for (int i = 0; i < k; ++i) {
    for (int j = i + 1; j < k; ++j) {
      if (xy[i].first.first != xy[j].first.first) {
        i = j - 1;
        break;
      }

      int cost = xy[j].first.second - xy[j - 1].first.second;
      int to = xy[j].second, from = xy[j - 1].second;
      G[from][0].PB({cost, {to, 0}});
      G[to][0].PB({cost, {from, 0}});
    }
  }

  for (int i = 0; i < k; ++i) {
    for (int j = i + 1; j < k; ++j) {
      if (yx[i].first.first != yx[j].first.first) {
        i = j - 1;
        break;
      }

      int cost = yx[j].first.second - yx[j - 1].first.second;
      int to = yx[j].second, from = yx[j - 1].second;
      G[from][1].PB({cost, {to, 1}});
      G[to][1].PB({cost, {from, 1}});
    }
  }

  cout << Dijkstra() << endl;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

#define int long long
#define PB push_back
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;

constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MAX_K = 200005;

int d[MAX_K][2], n, m, k;
vector<ppiii> xy, yx;
vector<pipii> G[MAX_K][2];

int Dijkstra() {
  fill(d[0], d[MAX_K], INF);
  d[k][0] = 0;
  priority_queue<pipii, vector<pipii>, greater<pipii>> pq;
  pq.push({0, {k, 0}});

  while (pq.size()) {
    pipii p = pq.top();
    pq.pop();
    int v = p.second.first;
    int st = p.second.second;
    if (d[v][st] < p.first)
      continue;

    for (int i = 0; i < G[v][st].size(); ++i) {
      int tov = G[v][st][i].second.first, tost = G[v][st][i].second.second;
      if (d[tov][tost] <= p.first + G[v][st][i].first)
        continue;
      d[tov][tost] = p.first + G[v][st][i].first;
      pq.push({d[tov][tost], {tov, tost}});
    }
  }

  int res = min(d[k + 1][0], d[k + 1][1]);
  return INF <= res ? -1 : res;
}

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

  cin >> m >> n >> k;

  for (int i = 0; i < k; ++i) {
    int x, y;
    cin >> x >> y;
    xy.PB({{x, y}, i});
    yx.PB({{y, x}, i});
    G[i][0].PB({1, {i, 1}});
    G[i][1].PB({1, {i, 0}});

    if (x == 1) {
      G[k][0].PB({y - 1, {i, 0}});
    }
    if (x == m) {
      G[i][0].PB({n - y, {k + 1, 0}});
    }
    if (y == n) {
      G[i][1].PB({m - x, {k + 1, 1}});
    }
  }
  sort(ALL(xy));
  sort(ALL(yx));

  for (int i = 0; i < k; ++i) {
    for (int j = i + 1; j < k; ++j) {
      if (xy[i].first.first != xy[j].first.first) {
        i = j - 1;
        break;
      }

      int cost = xy[j].first.second - xy[j - 1].first.second;
      int to = xy[j].second, from = xy[j - 1].second;
      G[from][0].PB({cost, {to, 0}});
      G[to][0].PB({cost, {from, 0}});
    }
  }

  for (int i = 0; i < k; ++i) {
    for (int j = i + 1; j < k; ++j) {
      if (yx[i].first.first != yx[j].first.first) {
        i = j - 1;
        break;
      }

      int cost = yx[j].first.second - yx[j - 1].first.second;
      int to = yx[j].second, from = yx[j - 1].second;
      G[from][1].PB({cost, {to, 1}});
      G[to][1].PB({cost, {from, 1}});
    }
  }

  cout << Dijkstra() << endl;
}