Problem p00528 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
using PLLL = pair<ll, PLL>;

ll MOD = 1e9 + 7;
int M, N, K;
vector<PII> X[100010], Y[100010];
PII pos[200010];
ll d[200010][2];
int g_n = -1;
int th_s_s = 0; // 0,0??????????????????????????????????????????

int main(void) {
  cin >> M >> N >> K;
  REP(i, 200010) {
    REP(j, 2) { d[i][j] = INF_LL; }
  }
  REP(i, K) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    X[x].push_back({y, i + 1});
    Y[y].push_back({x, i + 1});
    pos[i + 1] = {x, y};
    if (x == M - 1 && y == N - 1) {
      g_n = i + 1;
    }
    if (x == 0 && y == 0)
      th_s_s = 1;
  }
  if (g_n == -1) {
    X[M - 1].push_back({N - 1, K + 1});
    Y[N - 1].push_back({M - 1, K + 1});
    g_n = K + 1;
  }

  priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>,
                 greater<pair<ll, pair<int, int>>>>
      pq;
  d[0][0] = 0;
  REP(i, X[0].size()) {
    d[X[0][i].second][0] = X[0][i].first;
    d[X[0][i].second][1] = min(d[X[0][i].second][1], (ll)X[0][i].first + 1);
    pq.push({X[0][i].first + 1, {X[0][i].second, 1}});
  }
  if (th_s_s) {
    REP(i, Y[0].size()) {
      d[Y[0][i].second][1] = Y[0][i].first + 1;
      d[Y[0][i].second][0] = min(d[Y[0][i].second][0], (ll)Y[0][i].first + 2);
      pq.push({Y[0][i].first + 1, {Y[0][i].second, 0}});
    }
  }
  while (pq.size()) {
    pair<ll, pair<int, int>> p = pq.top();
    pq.pop();
    int v = p.second.first, s = p.second.second;

    if (d[v][s] < p.first)
      continue;
    if (s == 0) {
      int x_p = pos[v].first;
      REP(i, X[x_p].size()) {
        int dis = abs(X[x_p][i].first - pos[v].second);
        if (d[X[x_p][i].second][s] > d[v][s] + dis) {
          d[X[x_p][i].second][s] = d[v][s] + dis;
          d[X[x_p][i].second][(s + 1) % 2] =
              min(d[X[x_p][i].second][(s + 1) % 2], d[X[x_p][i].second][s] + 1);
          pq.push({d[X[x_p][i].second][(s + 1) % 2],
                   {X[x_p][i].second, (s + 1) % 2}});
        }
      }
    } else {
      int y_p = pos[v].second;
      REP(i, Y[y_p].size()) {
        int dis = abs(Y[y_p][i].first - pos[v].first);
        if (d[Y[y_p][i].second][s] > d[v][s] + dis) {
          d[Y[y_p][i].second][s] = d[v][s] + dis;
          d[Y[y_p][i].second][(s + 1) % 2] =
              min(d[Y[y_p][i].second][(s + 1) % 2], d[Y[y_p][i].second][s] + 1);
          pq.push({d[Y[y_p][i].second][(s + 1) % 2],
                   {Y[y_p][i].second, (s + 1) % 2}});
        }
      }
    }
  }
  if (d[g_n][0] == INF_LL && d[g_n][1] == INF_LL) {
    cout << -1 << endl;
  } else {
    cout << min(d[g_n][0], d[g_n][1]) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
using PLLL = pair<ll, PLL>;

ll MOD = 1e9 + 7;
int M, N, K;
vector<PII> X[100010], Y[100010];
PII pos[100010];
ll d[100010][2];
int g_n = -1;
int th_s_s = 0; // 0,0??????????????????????????????????????????

int main(void) {
  cin >> M >> N >> K;
  REP(i, 100010) {
    REP(j, 2) { d[i][j] = INF_LL; }
  }
  REP(i, K) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    X[x].push_back({y, i + 1});
    Y[y].push_back({x, i + 1});
    pos[i + 1] = {x, y};
    if (x == M - 1 && y == N - 1) {
      g_n = i + 1;
    }
    if (x == 0 && y == 0)
      th_s_s = 1;
  }
  if (g_n == -1) {
    X[M - 1].push_back({N - 1, K + 1});
    Y[N - 1].push_back({M - 1, K + 1});
    g_n = K + 1;
  }

  priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>,
                 greater<pair<ll, pair<int, int>>>>
      pq;
  d[0][0] = 0;
  REP(i, X[0].size()) {
    d[X[0][i].second][0] = X[0][i].first;
    d[X[0][i].second][1] = min(d[X[0][i].second][1], (ll)X[0][i].first + 1);
    pq.push({X[0][i].first + 1, {X[0][i].second, 1}});
  }
  if (th_s_s) {
    REP(i, Y[0].size()) {
      d[Y[0][i].second][1] = Y[0][i].first + 1;
      d[Y[0][i].second][0] = min(d[Y[0][i].second][0], (ll)Y[0][i].first + 2);
      pq.push({Y[0][i].first + 1, {Y[0][i].second, 0}});
    }
  }
  while (pq.size()) {
    pair<ll, pair<int, int>> p = pq.top();
    pq.pop();
    int v = p.second.first, s = p.second.second;

    if (d[v][s] < p.first)
      continue;
    if (s == 0) {
      int x_p = pos[v].first;
      REP(i, X[x_p].size()) {
        int dis = abs(X[x_p][i].first - pos[v].second);
        if (d[X[x_p][i].second][s] > d[v][s] + dis) {
          d[X[x_p][i].second][s] = d[v][s] + dis;
          d[X[x_p][i].second][(s + 1) % 2] =
              min(d[X[x_p][i].second][(s + 1) % 2], d[X[x_p][i].second][s] + 1);
          pq.push({d[X[x_p][i].second][(s + 1) % 2],
                   {X[x_p][i].second, (s + 1) % 2}});
        }
      }
    } else {
      int y_p = pos[v].second;
      REP(i, Y[y_p].size()) {
        int dis = abs(Y[y_p][i].first - pos[v].first);
        if (d[Y[y_p][i].second][s] > d[v][s] + dis) {
          d[Y[y_p][i].second][s] = d[v][s] + dis;
          d[Y[y_p][i].second][(s + 1) % 2] =
              min(d[Y[y_p][i].second][(s + 1) % 2], d[Y[y_p][i].second][s] + 1);
          pq.push({d[Y[y_p][i].second][(s + 1) % 2],
                   {Y[y_p][i].second, (s + 1) % 2}});
        }
      }
    }
  }
  if (d[g_n][0] == INF_LL && d[g_n][1] == INF_LL) {
    cout << -1 << endl;
  } else {
    cout << min(d[g_n][0], d[g_n][1]) << endl;
  }
  return 0;
}