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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll, ll> pint;

pair<pint, int> vx[200005], vy[200005];
vector<pint> g[400005];
ll d[400005];
ll INF = 1000100010001000100;
int n, m;
int dijkstra(int st) {
  priority_queue<pint, vector<pint>, greater<pint>> pq;
  rep(i, 400005) d[i] = INF;
  d[st] = 0;
  pq.push(make_pair(0, st));
  while (!pq.empty()) {
    pint pi = pq.top();
    pq.pop();
    int t = pi.second;
    if (d[t] < pi.first)
      continue;
    for (vector<pint>::iterator it = g[t].begin(); it != g[t].end(); ++it) {
      if (d[it->second] > d[t] + it->first) {
        d[it->second] = d[t] + it->first;
        pq.push(make_pair(d[it->second], it->second));
      }
    }
  }
}
int main() {
  int k, xi, yi;
  cin >> m >> n >> k;
  FOR(i, 1, k + 1) {
    cin >> xi >> yi;
    --xi, --yi;
    vx[i] = {{xi, yi}, i}, vy[i - 1] = {{yi, xi}, k + i + 1};
    g[i].pb(1, k + i + 1);
    g[i + k + 1].pb(1, i);
  }
  vx[k + 1] = {{m - 1, n - 1}, k + 1}, vy[k] = {{n - 1, m - 1}, k + k + 2};
  g[k + 1].pb(1, k + k + 2);
  g[k + k + 2].pb(1, k + 1);
  sort(vx, vx + k + 2);
  sort(vy, vy + k + 1);
  rep(i, k + 1) {
    if (vx[i].first.first == vx[i + 1].first.first) {
      g[vx[i].second].pb(abs(vx[i + 1].first.second - vx[i].first.second),
                         vx[i + 1].second);
      g[vx[i + 1].second].pb(abs(vx[i + 1].first.second - vx[i].first.second),
                             vx[i].second);
    }
  }
  rep(i, k) {
    if (vy[i].first.first == vy[i + 1].first.first) {
      g[vy[i].second].pb(abs(vy[i + 1].first.second - vy[i].first.second),
                         vy[i + 1].second);
      g[vy[i + 1].second].pb(abs(vy[i + 1].first.second - vy[i].first.second),
                             vy[i].second);
    }
  }
  dijkstra(0);
  ll ans = min(d[k + 1], d[k + k + 2]);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll, ll> pint;

pair<pint, int> vx[200005], vy[200005];
vector<pint> g[200005];
ll d[200005];
ll INF = 1000100010001000100;
int n, m;
int dijkstra(int st) {
  priority_queue<pint, vector<pint>, greater<pint>> pq;
  rep(i, 200005) d[i] = INF;
  d[st] = 0;
  pq.push(make_pair(0, st));
  while (!pq.empty()) {
    pint pi = pq.top();
    pq.pop();
    int t = pi.second;
    if (d[t] < pi.first)
      continue;
    for (vector<pint>::iterator it = g[t].begin(); it != g[t].end(); ++it) {
      if (d[it->second] > d[t] + it->first) {
        d[it->second] = d[t] + it->first;
        pq.push(make_pair(d[it->second], it->second));
      }
    }
  }
}
int main() {
  int k, xi, yi;
  cin >> m >> n >> k;
  FOR(i, 1, k + 1) {
    cin >> xi >> yi;
    --xi, --yi;
    vx[i] = {{xi, yi}, i}, vy[i - 1] = {{yi, xi}, k + i + 1};
    g[i].pb(1, k + i + 1);
    g[i + k + 1].pb(1, i);
  }
  vx[k + 1] = {{m - 1, n - 1}, k + 1}, vy[k] = {{n - 1, m - 1}, k + k + 2};
  g[k + 1].pb(1, k + k + 2);
  g[k + k + 2].pb(1, k + 1);
  sort(vx, vx + k + 2);
  sort(vy, vy + k + 1);
  rep(i, k + 1) {
    if (vx[i].first.first == vx[i + 1].first.first) {
      g[vx[i].second].pb(abs(vx[i + 1].first.second - vx[i].first.second),
                         vx[i + 1].second);
      g[vx[i + 1].second].pb(abs(vx[i + 1].first.second - vx[i].first.second),
                             vx[i].second);
    }
  }
  rep(i, k) {
    if (vy[i].first.first == vy[i + 1].first.first) {
      g[vy[i].second].pb(abs(vy[i + 1].first.second - vy[i].first.second),
                         vy[i + 1].second);
      g[vy[i + 1].second].pb(abs(vy[i + 1].first.second - vy[i].first.second),
                             vy[i].second);
    }
  }
  dijkstra(0);
  ll ans = min(d[k + 1], d[k + k + 2]);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}