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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<ll, ll, ll, ll, ll> tlg5;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
  if (ok) {
    cout << yes << endl;
  } else {
    cout << no << endl;
  }
}

bool cmp(plglg p, plglg q) {
  if (p.first == q.first) {
    return p.second > q.second;
  } else {
    return p.first < q.first;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll H, W, K;
  cin >> H >> W >> K;
  bool bo[H][W];
  plglg d[H][W][4];
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  REP(i, 0, H) {
    string s;
    cin >> s;
    REP(j, 0, W) {
      if (s[j] == '@') {
        bo[i][j] = false;
      } else {
        bo[i][j] = true;
      }
    }
  }
  REP(i, 0, H) {
    REP(j, 0, W) { fill(d[i][j], d[i][j] + 4, plglg(inf, 0)); }
  }
  auto c = [](tlg5 l, tlg5 r) {
    if (get<0>(l) == get<0>(r)) {
      return get<1>(l) < get<1>(r);
    } else {
      return get<0>(l) > get<0>(r);
    }
  };
  priority_queue<tlg5, V<tlg5>, decltype(c)> que(c);
  REP(i, 0, 4) {
    que.push(tlg5(0, 0, x1, y1, i));
    d[x1][y1][i] = plglg(0, 0);
  }
  while (!que.empty()) {
    tlg5 t = que.top();
    ll dis = get<0>(t);
    ll rem = get<1>(t);
    ll x = get<2>(t);
    ll y = get<3>(t);
    ll dir = get<4>(t);
    que.pop();
    if (cmp(d[x][y][dir], plglg(dis, rem))) {
      continue;
    }
    REP(i, 0, 4) {
      ll newx = x + dx[i];
      ll newy = y + dy[i];
      if (newx < 0 || newx >= H || newy < 0 || newy >= W) {
        continue;
      }
      if (!bo[newx][newy]) {
        continue;
      }
      ll newdis = (i == dir && rem > 0) ? dis : dis + 1;
      ll newrem = (i == dir && rem > 0) ? rem - 1 : K - 1;
      if (cmp(plglg(newdis, newrem), d[newx][newy][i])) {
        d[newx][newy][i] = plglg(newdis, newrem);
        que.push(tlg5(newdis, newrem, newx, newy, i));
      }
    }
  }
  // REP(i, 0, H) {
  //     REP(j, 0, W) {
  //         REP(k, 0, 4) {
  //             cout << d[i][j][k].first << " " << d[i][j][k].second << " aaa
  //             ";
  //         }
  //     }
  //     cout << " bbb " << endl;
  // }
  ll ans = inf;
  REP(i, 0, 4) { ans = min(ans, d[x2][y2][i].first); }
  if (ans < inf) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<ll, ll, ll, ll, ll> tlg5;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
  if (ok) {
    cout << yes << endl;
  } else {
    cout << no << endl;
  }
}

bool cmp(plglg p, plglg q) {
  if (p.first == q.first) {
    return p.second > q.second;
  } else {
    return p.first < q.first;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll H, W, K;
  cin >> H >> W >> K;
  bool bo[H + 20][W + 20];
  plglg d[H + 10][W + 20][5];
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  REP(i, 0, H) {
    string s;
    cin >> s;
    REP(j, 0, W) {
      if (s[j] == '@') {
        bo[i][j] = false;
      } else {
        bo[i][j] = true;
      }
    }
  }
  REP(i, 0, H) {
    REP(j, 0, W) { fill(d[i][j], d[i][j] + 4, plglg(inf, 0)); }
  }
  auto c = [](tlg5 l, tlg5 r) {
    if (get<0>(l) == get<0>(r)) {
      return get<1>(l) < get<1>(r);
    } else {
      return get<0>(l) > get<0>(r);
    }
  };
  priority_queue<tlg5, V<tlg5>, decltype(c)> que(c);
  REP(i, 0, 4) {
    que.push(tlg5(0, 0, x1, y1, i));
    d[x1][y1][i] = plglg(0, 0);
  }
  while (!que.empty()) {
    tlg5 t = que.top();
    ll dis = get<0>(t);
    ll rem = get<1>(t);
    ll x = get<2>(t);
    ll y = get<3>(t);
    ll dir = get<4>(t);
    que.pop();
    if (cmp(d[x][y][dir], plglg(dis, rem))) {
      continue;
    }
    REP(i, 0, 4) {
      ll newx = x + dx[i];
      ll newy = y + dy[i];
      if (!bo[newx][newy] || newx < 0 || newx >= H || newy < 0 || newy >= W) {
        continue;
      }
      ll newdis = (i == dir && rem > 0) ? dis : dis + 1;
      ll newrem = (i == dir && rem > 0) ? rem - 1 : K - 1;
      if (cmp(plglg(newdis, newrem), d[newx][newy][i])) {
        d[newx][newy][i] = plglg(newdis, newrem);
        que.push(tlg5(newdis, newrem, newx, newy, i));
      }
    }
  }
  // REP(i, 0, H) {
  //     REP(j, 0, W) {
  //         REP(k, 0, 4) {
  //             cout << d[i][j][k].first << " " << d[i][j][k].second << " aaa
  //             ";
  //         }
  //     }
  //     cout << " bbb " << endl;
  // }
  ll ans = inf;
  REP(i, 0, 4) { ans = min(ans, d[x2][y2][i].first); }
  if (ans < inf) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}