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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I)
#define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x, t, f) ((x) ? (t) : (f))
#define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x, v)                                                             \\
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v  x is sorted
#define POSU(x, v)                                                             \\
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
#define REV(x) (reverse(x.begin(), x.end()))       // reverse
ll gcd_(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd_(b, a % b);
}
ll lcm_(ll a, ll b) {
  ll c = gcd_(a, b);
  return ((a / c) * (b / c) * c);
}
#define NEXTP(x) next_permutation(x.begin(), x.end())
const ll INF = ll(1e16) + ll(7);
const ll MOD = 1000000007LL;
#define out(a) cout << fixed << setprecision((a))
// tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a) {
  ll r = 0;
  while (a) {
    a /= 10;
    r++;
  }
  return r;
}

class dijkstra {
  // 0indexed
public:
  int v, startv;
  vector<int> d;
  vector<vector<pair<int, int>>> e;

  void initsize(int n0) {
    d.resize(n0);
    for (int i = 0; i < n0; i++)
      d[i] = INT_MAX;
    vector<pair<int, int>> ep;
    for (int i = 0; i < n0; i++)
      e.push_back(ep);
    v = n0;
  }

  void initstart(int s) {
    startv = s;
    d[s] = 0;
  }

  void make_edge(int x, int y, int cost) {
    e[x].push_back(make_pair(y, cost));
    e[y].push_back(make_pair(x, cost));
  }

  void make_edgedir(int x, int y, int cost) {
    e[x].push_back(make_pair(y, cost));
  }

  void calcdistance() {
    // <-cost,x>
    // cost startv->x
    priority_queue<pair<int, int>> q;
    pair<int, int> p;
    vector<bool> did;
    for (int i = 0; i < v; i++)
      did.push_back(false);
    did[startv] = true;
    for (int i = 0; i < (int)e[startv].size(); i++) {
      p.first = -e[startv][i].second;
      p.second = e[startv][i].first;
      q.push(p);
    }
    while (q.size() != 0) {
      pair<int, int> p;
      p = q.top();
      q.pop();
      int x, costsx;
      x = p.second;
      costsx = -p.first;
      if (did[x] == true)
        continue;
      did[x] = true;
      d[x] = costsx;
      for (int i = 0; i < (int)e[x].size(); i++) {
        p.first = -(d[x] + e[x][i].second);
        p.second = e[x][i].first;
        if (did[p.second] == false)
          q.push(p);
      }
    }
  }
};

ll H, W;
ll ind(ll h, ll w) { return h + w * H; }

int main() {

  cin >> H >> W;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<string> S(H);
  FOR(i, 0, H) cin >> S[i];

  dijkstra di;
  di.initsize(H * W + 100);
  FOR(h, 0, H) {
    FOR(w, 0, W) {
      if (S[h][w] == '#')
        continue;

      for (int i = h - 1; i >= 0; i--) {
        if (S[i][w] == '#')
          break;
        di.make_edgedir(ind(i, w), ind(h, w), 0);
      }
      for (int i = h + 1; i < H; i++) {
        if (S[i][w] == '#')
          break;
        di.make_edgedir(ind(i, w), ind(h, w), 0);
      }
      for (int i = w - 1; i >= 0; i--) {
        if (S[h][i] == '#')
          break;
        di.make_edgedir(ind(h, i), ind(h, w), 0);
      }
      for (int i = w + 1; i < W; i++) {
        if (S[h][i] == '#')
          break;
        di.make_edgedir(ind(h, i), ind(h, w), 0);
      }

      FOR(i, -2, 3) FOR(j, -2, 3) {
        if (i == 0 && j == 0)
          continue;
        if (h + i < 0 || h + i >= H || w + j < 0 || w + j >= W)
          continue;
        di.make_edgedir(ind(h, w), ind(h + i, w + j), 1);
      }
    }
  }
  di.initstart(ind(ch, cw));
  di.calcdistance();
  if (di.d[ind(dh, dw)] == INT_MAX)
    di.d[ind(dh, dw)] = -1;
  cout << di.d[ind(dh, dw)] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I)
#define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x, t, f) ((x) ? (t) : (f))
#define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x, v)                                                             \\
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v  x is sorted
#define POSU(x, v)                                                             \\
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
#define REV(x) (reverse(x.begin(), x.end()))       // reverse
ll gcd_(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd_(b, a % b);
}
ll lcm_(ll a, ll b) {
  ll c = gcd_(a, b);
  return ((a / c) * (b / c) * c);
}
#define NEXTP(x) next_permutation(x.begin(), x.end())
const ll INF = ll(1e16) + ll(7);
const ll MOD = 1000000007LL;
#define out(a) cout << fixed << setprecision((a))
// tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a) {
  ll r = 0;
  while (a) {
    a /= 10;
    r++;
  }
  return r;
}

class dijkstra {
  // 0indexed
public:
  int v, startv;
  vector<int> d;
  vector<vector<pair<int, int>>> e;

  void initsize(int n0) {
    d.resize(n0);
    for (int i = 0; i < n0; i++)
      d[i] = INT_MAX;
    vector<pair<int, int>> ep;
    for (int i = 0; i < n0; i++)
      e.push_back(ep);
    v = n0;
  }

  void initstart(int s) {
    startv = s;
    d[s] = 0;
  }

  void make_edge(int x, int y, int cost) {
    e[x].push_back(make_pair(y, cost));
    e[y].push_back(make_pair(x, cost));
  }

  void make_edgedir(int x, int y, int cost) {
    e[x].push_back(make_pair(y, cost));
  }

  void calcdistance() {
    // <-cost,x>
    // cost startv->x
    priority_queue<pair<int, int>> q;
    pair<int, int> p;
    vector<bool> did;
    for (int i = 0; i < v; i++)
      did.push_back(false);
    did[startv] = true;
    for (int i = 0; i < e[startv].size(); i++) {
      p.first = -e[startv][i].second;
      p.second = e[startv][i].first;
      q.push(p);
    }
    while (q.size() != 0) {
      pair<int, int> p;
      p = q.top();
      q.pop();
      int x, costsx;
      x = p.second;
      costsx = -p.first;
      if (did[x] == true)
        continue;
      did[x] = true;
      d[x] = costsx;
      for (int i = 0; i < e[x].size(); i++) {
        p.first = -(d[x] + e[x][i].second);
        p.second = e[x][i].first;
        if (did[p.second] == false)
          q.push(p);
      }
    }
  }
};

ll H, W;
ll ind(ll h, ll w) { return h + w * H; }

int main() {

  cin >> H >> W;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<string> S(H);
  FOR(i, 0, H) cin >> S[i];

  dijkstra di;
  di.initsize(H * W + 100);
  FOR(h, 0, H) {
    FOR(w, 0, W) {
      if (S[h][w] == '#')
        continue;

      for (int i = h - 1; i >= 0; i--) {
        if (S[i][w] == '#')
          break;
        di.make_edgedir(ind(i, w), ind(h, w), 0);
      }
      for (int i = h + 1; i < H; i++) {
        if (S[i][w] == '#')
          break;
        di.make_edgedir(ind(i, w), ind(h, w), 0);
      }
      for (int i = w - 1; i >= 0; i--) {
        if (S[h][i] == '#')
          break;
        di.make_edgedir(ind(h, i), ind(h, w), 0);
      }
      for (int i = w + 1; i < W; i++) {
        if (S[h][i] == '#')
          break;
        di.make_edgedir(ind(h, i), ind(h, w), 0);
      }

      FOR(i, -2, 3) FOR(j, -2, 3) {
        if (i == 0 && j == 0)
          continue;
        if (h + i < 0 || h + i > H || w + j < 0 || w + j > W)
          continue;
        di.make_edgedir(ind(h, w), ind(h + i, w + j), 1);
      }
    }
  }
  di.initstart(ind(ch, cw));
  di.calcdistance();
  if (di.d[ind(dh, dw)] == INT_MAX)
    di.d[ind(dh, dw)] = -1;
  cout << di.d[ind(dh, dw)] << endl;
}