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

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

#define int ll

int h, w, k;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
vector<string> c;

template <typename T> struct edge {
  int to;
  T cost;
  edge(int to, T cost) : to(to), cost(cost) {}
};

template <typename T>
vector<vector<vector<int>>> dijkstra(int sx, int sy) { // sは始点
  const auto INFTY =
      numeric_limits<int>::max() /
      10; // 2回以上使うときはオーバーフローに気をつける(INFTYのときはif文で弾くなど)
  vector<vector<vector<int>>> dist(
      h, vector<vector<int>>(w, vector<int>(4, INFTY)));
  using PP = pair<int, int>;
  using P = pair<int, pair<PP, int>>; // firstは最短距離,secondは頂点の番号
  priority_queue<P, vector<P>, greater<P>> pq;
  rep(i, 4) dist[sx][sy][i] = 0;
  rep(i, 4) pq.emplace(dist[sx][sy][i], make_pair(PP{sx, sy}, i));
  while (!pq.empty()) {
    auto x = pq.top();
    pq.pop();
    int vx = x.second.first.first, vy = x.second.first.second;
    int dir = x.second.second;
    if (dist[vx][vy][dir] < x.first)
      continue; // 最短距離でなければ飛ばす
    rep(i, 4) {
      int nx = vx + dx[i], ny = vy + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (c[nx][ny] == '@')
        continue;
      if (dir == i) {
        if (chmin(dist[nx][ny][i], dist[vx][vy][dir] + 1)) {
          pq.emplace(dist[nx][ny][i], make_pair(PP{nx, ny}, i));
        }
      } else {
        if (chmin(dist[nx][ny][i], (dist[vx][vy][dir] + k - 1) / k * k + 1)) {
          pq.emplace(dist[nx][ny][i], make_pair(PP{nx, ny}, i));
        }
      }
    }
  }
  return dist;
}

void solve() {
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  c.resize(h);
  rep(i, h) cin >> c[i];
  const int INFTY = numeric_limits<int>::max() / 10;
  auto d = dijkstra<int>(x1, y1);
  // rep(i,h){
  // 	rep(j,w){
  // 		int s=INFTY;
  // 		chmin(s,d[i][j]);
  // 		if(s==INFTY){
  // 			cout<<'@';
  // 		}
  // 		else cout<<s;
  // 		cout<<" ";
  // 	}
  // 	cout<<"\\n";
  // }
  int ans = INFTY;
  rep(i, 4) chmin(ans, d[x2][y2][i]);
  if (ans == INFTY)
    cout << -1;
  else
    cout << (ans + k - 1) / k;
}

#undef int
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: -6
free(): invalid size


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll gcd(ll n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

#define int ll

int h, w, k;
vector<int> d;
vector<string> c;

template <typename T> struct edge {
  int to;
  T cost;
  edge(int to, T cost) : to(to), cost(cost) {}
};

template <typename T> vector<int> dijkstra(int s) { // sは始点
  const auto INFTY =
      numeric_limits<int>::max() /
      10; // 2回以上使うときはオーバーフローに気をつける(INFTYのときはif文で弾くなど)
  vector<int> dist(h * w, INFTY);
  using P = pair<int, pair<int, int>>; // firstは最短距離,secondは頂点の番号
  priority_queue<P, vector<P>, greater<P>> pq;
  dist[s] = 0;
  rep(i, 4) pq.emplace(dist[s], make_pair(s, i));
  while (!pq.empty()) {
    auto x = pq.top();
    pq.pop();
    int v = x.second.first;
    int from = x.second.second;
    if (dist[v] < x.first)
      continue; // 最短距離でなければ飛ばす
    rep(i, 4) {
      int nx = v + d[i];
      if (nx / w >= h)
        continue;
      if (nx / w < 0)
        continue;
      if ((v + w) % w == 0 && (nx + w) % w == w - 1)
        continue;
      if ((v + w) % w == w - 1 && (nx + w) % w == 0)
        continue;
      if (c[nx / w][nx % w] == '@')
        continue;
      cout << v << " " << nx << "\\n";
      if (from == i) {
        if (dist[nx] > dist[v] + 1) {
          dist[nx] = dist[v] + 1;
          pq.emplace(dist[nx], make_pair(nx, i));
        }
      } else {
        if (dist[nx] > (dist[v] + k - 1) / k * k + 1) {
          dist[nx] = (dist[v] + k - 1) / k * k + 1;
          pq.emplace(dist[nx], make_pair(nx, i));
        }
      }
    }
  }
  return dist;
}

using P = pair<int, int>;

void solve() {
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  c.resize(h);
  rep(i, h) cin >> c[i];
  const int INFTY = numeric_limits<int>::max() / 10;
  d.pb(w);
  d.pb(-w);
  d.pb(1);
  d.pb(-1);
  auto dist = dijkstra<P>(x1 * w + y1);
  // rep(i,h){
  // 	rep(j,w){
  // 		int s=INFTY;
  // 		chmin(s,d[i*w+j]);
  // 		if(s==INFTY){
  // 			cout<<'@';
  // 		}
  // 		else cout<<s;
  // 		cout<<" ";
  // 	}
  // 	cout<<"\\n";
  // }
  int ans = INFTY;
  chmin(ans, dist[x2 * w + y2]);
  if (ans == INFTY)
    cout << -1;
  else
    cout << ((ans + k - 1) / k * k) / k;
}

#undef int
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}