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

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long int LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

struct room {
  int num;
  bool visited[2];

  bool operator<(const room r) const { return num < r.num; }
  bool operator>(const room r) const { return num > r.num; }
};

struct r_compare {
  LL cost;
  int loc_x, loc_y;
  bool sw;

  bool operator<(const r_compare r) const { return cost < r.cost; }
  bool operator>(const r_compare r) const { return cost > r.cost; }
};

int m, n, k;
vector<room> ns[100010], ew[100010];
priority_queue<r_compare, vector<r_compare>, greater<r_compare>> Q;

LL dijk() {
  if (ns[1].empty())
    return -1;

  r_compare qin = {(ns[1].begin())->num - 1, 1, (ns[1].begin())->num, false};
  Q.push(qin);

  while (!Q.empty()) {
    r_compare c_now = Q.top();
    Q.pop();

    if (c_now.loc_x == m && c_now.loc_y == n)
      return c_now.cost;

    room sr = {(!c_now.sw ? c_now.loc_y : c_now.loc_x), {false, false}};
    vector<room>::iterator r_now =
        !c_now.sw
            ? lower_bound(ns[c_now.loc_x].begin(), ns[c_now.loc_x].end(), sr)
            : lower_bound(ew[c_now.loc_y].begin(), ew[c_now.loc_y].end(), sr);

    if (!r_now->visited[c_now.sw]) {
      r_now->visited[c_now.sw] = true;

      if (!c_now.sw) {
        if (r_now != ns[c_now.loc_x].begin()) {
          r_compare qin = {c_now.cost + r_now->num - (r_now - 1)->num,
                           c_now.loc_x, (r_now - 1)->num, false};
          Q.push(qin);
        }
        if (r_now != ns[c_now.loc_x].end() - 1) {
          r_compare qin = {c_now.cost + (r_now + 1)->num - r_now->num,
                           c_now.loc_x, (r_now + 1)->num, false};
          Q.push(qin);
        }
      } else {
        if (r_now != ew[c_now.loc_y].begin()) {
          r_compare qin = {c_now.cost + r_now->num - (r_now - 1)->num,
                           (r_now - 1)->num, c_now.loc_y, true};
          Q.push(qin);
        }
        if (r_now != ew[c_now.loc_y].end() - 1) {
          r_compare qin = {c_now.cost + (r_now + 1)->num - r_now->num,
                           (r_now + 1)->num, c_now.loc_y, true};
          Q.push(qin);
        }
      }

      r_compare qin = {c_now.cost + 1, c_now.loc_x, c_now.loc_y, !c_now.sw};
      Q.push(qin);
    }
  }
  return -1;
}

int main() {
  scanf("%d%d%d", &m, &n, &k);

  bool last = false;
  for (int i = 0; i < k; i++) {
    int x, y;
    if (x == m && y == n)
      last = true;
    scanf("%d%d", &x, &y);
    room xin = {y, {false, false}}, yin = {x, {false, false}};
    ns[x].PB(xin);
    ew[y].PB(yin);
  }

  if (!last) {
    room xin = {n, {false, false}}, yin = {m, {false, false}};
    ns[m].PB(xin);
    ew[n].PB(yin);
  }

  for (int i = 1; i <= m; i++)
    sort(ns[i].begin(), ns[i].end());
  for (int i = 1; i <= n; i++)
    sort(ew[i].begin(), ew[i].end());

  printf("%lld\\n", dijk());
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long int LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

struct room {
  int num;
  bool visited[2];

  bool operator<(const room r) const { return num < r.num; }
  bool operator>(const room r) const { return num > r.num; }
};

struct r_compare {
  LL cost;
  int loc_x, loc_y;
  bool sw;

  bool operator<(const r_compare r) const { return cost < r.cost; }
  bool operator>(const r_compare r) const { return cost > r.cost; }
};

int m, n, k;
vector<room> ns[100010], ew[100010];
priority_queue<r_compare, vector<r_compare>, greater<r_compare>> Q;

LL dijk() {
  if (ns[1].size() == 0)
    return -1;

  r_compare qin = {(ns[1].begin())->num - 1, 1, (ns[1].begin())->num, false};
  Q.push(qin);

  while (!Q.empty()) {
    r_compare c_now = Q.top();
    Q.pop();

    if (c_now.loc_x == m && c_now.loc_y == n)
      return c_now.cost;

    room sr = {(!c_now.sw ? c_now.loc_y : c_now.loc_x), {false, false}};
    vector<room>::iterator r_now =
        !c_now.sw
            ? lower_bound(ns[c_now.loc_x].begin(), ns[c_now.loc_x].end(), sr)
            : lower_bound(ew[c_now.loc_y].begin(), ew[c_now.loc_y].end(), sr);

    if (!r_now->visited[c_now.sw]) {
      r_now->visited[c_now.sw] = true;

      if (!c_now.sw) {
        if (r_now != ns[c_now.loc_x].begin()) {
          r_compare qin = {c_now.cost + r_now->num - (r_now - 1)->num,
                           c_now.loc_x, (r_now - 1)->num, false};
          Q.push(qin);
        }
        if (r_now != ns[c_now.loc_x].end() - 1) {
          r_compare qin = {c_now.cost + (r_now + 1)->num - r_now->num,
                           c_now.loc_x, (r_now + 1)->num, false};
          Q.push(qin);
        }
      } else {
        if (r_now != ew[c_now.loc_y].begin()) {
          r_compare qin = {c_now.cost + r_now->num - (r_now - 1)->num,
                           (r_now - 1)->num, c_now.loc_y, true};
          Q.push(qin);
        }
        if (r_now != ew[c_now.loc_y].end() - 1) {
          r_compare qin = {c_now.cost + (r_now + 1)->num - r_now->num,
                           (r_now + 1)->num, c_now.loc_y, true};
          Q.push(qin);
        }
      }

      r_compare qin = {c_now.cost + 1, c_now.loc_x, c_now.loc_y, !c_now.sw};
      Q.push(qin);
    }
  }
  return -1;
}

int main() {
  scanf("%d%d%d", &m, &n, &k);

  bool last = false;
  for (int i = 0; i < k; i++) {
    int x, y;
    if (x == m && y == n)
      last = true;
    scanf("%d%d", &x, &y);
    room xin = {y, {false, false}}, yin = {x, {false, false}};
    ns[x].PB(xin);
    ew[y].PB(yin);
  }

  if (!last) {
    room xin = {n, {false, false}}, yin = {m, {false, false}};
    ns[m].PB(xin);
    ew[n].PB(yin);
  }

  for (int i = 1; i <= m; i++)
    sort(ns[i].begin(), ns[i].end());
  for (int i = 1; i <= m; i++)
    sort(ew[i].begin(), ew[i].end());

  printf("%lld\\n", dijk());
  return 0;
}