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

#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef pair<lld, lld> P;
#define INF ((lld)1 << 55)

lld M, N, K;
vector<P> X[100010], Y[100010];
vector<P> node[400010];
lld min_cost[400010];

int main() {
  cin >> M >> N >> K;
  for (int i = 0; i < K; i++) {
    lld A, B;
    cin >> A >> B;
    X[A].push_back(P(B, i));
    Y[B].push_back(P(A, i));
  }
  X[1].push_back(P(1, K));
  Y[1].push_back(P(1, K));
  X[M].push_back(P(N, K + 1));
  Y[N].push_back(P(M, K + 1));
  K += 2;
  for (int i = 1; i <= M; i++) {
    sort(X[i].begin(), X[i].end());
    for (int j = 1; j < X[i].size(); j++) {
      P p1 = X[i][j - 1], p2 = X[i][j];
      node[p1.second].push_back(P(p2.second, llabs(p1.first - p2.first)));
      node[p2.second].push_back(P(p1.second, llabs(p1.first - p2.first)));
    }
  }
  for (int i = 1; i <= N; i++) {
    sort(Y[i].begin(), Y[i].end());
    for (int j = 1; j < Y[i].size(); j++) {
      P p1 = Y[i][j - 1], p2 = Y[i][j];
      node[p1.second + K].push_back(
          P(p2.second + K, llabs(p1.first - p2.first)));
      node[p2.second + K].push_back(
          P(p1.second + K, llabs(p1.first - p2.first)));
    }
  }
  for (int i = 0; i < K - 2; i++) {
    node[i].push_back(P(i + K, 1));
    node[i + K].push_back(P(i, 1));
  }
  fill_n(min_cost, 400010, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  min_cost[K - 2] = 0;
  que.push(P(0, K - 2));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    lld cost = p.first, pos = p.second;
    if (pos == K - 1 || pos == 2 * K - 1) {
      cout << cost << endl;
      exit(0);
    }
    if (min_cost[pos] < cost)
      continue;
    for (int i = 0; i < node[pos].size(); i++) {
      P pi = node[pos][i];
      lld next = pi.first, dis = pi.second;
      if (min_cost[next] > cost + dis) {
        min_cost[next] = cost + dis;
        que.push(P(cost + dis, next));
      }
    }
  }
  cout << "-1" << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
// typedef pair<int,int> P;
typedef pair<lld, lld> P;
typedef pair<P, lld> Pii;
typedef pair<P, P> Pi;
lld n, m, k;
vector<int> S[2][100010];
map<Pii, bool> used;

int main() {
  cin >> n >> m >> k;
  int flg = 0;
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a == 0 && b == 0)
      flg = 1;
    S[0][a].push_back(b);
    S[1][b].push_back(a);
  }
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  que.push(Pi(P(0, 0), P(0, 0)));
  if (flg)
    que.push(Pi(P(1, 1), P(0, 0)));
  while (!que.empty()) {
    Pi p = que.top();
    que.pop();
    lld cnt = p.first.first;
    bool vec = p.first.second;
    lld x = p.second.first;
    lld y = p.second.second;
    // if(used[Pii(P(x,y),vec)] != 0) continue;
    used[Pii(P(x, y), vec)] = 1;
    if (vec == 0 && x == n - 1) {
      cout << cnt + llabs((m - 1) - y) << endl;
      exit(0);
    }
    if (vec == 1 && y == m - 1) {
      cout << cnt + llabs((n - 1) - x) << endl;
      exit(0);
    }
    if (vec) {
      for (int i = 0; i < S[vec][y].size(); i++) {
        int next = S[vec][y][i];
        if (used[Pii(P(next, y), vec)] != 0)
          continue;
        que.push(Pi(P(cnt + llabs((lld)next - x) + 1, 0), P(next, y)));
      }
    } else {
      for (int i = 0; i < S[vec][x].size(); i++) {
        int next = S[vec][x][i];
        if (used[Pii(P(x, next), vec)] != 0)
          continue;
        que.push(Pi(P(cnt + llabs((lld)next - y) + 1, 1), P(x, next)));
      }
    }
  }
  cout << "-1" << endl;
}