Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

class state {
public:
  int x, y, d;
  ll c;
  state() {}
  state(int xx, int yy, int dd, ll cc) {
    x = xx;
    y = yy;
    d = dd;
    c = cc;
  }
  bool operator<(const state &s) const { return c < s.c; }
};

int h, w, k;
int sx, sy, gx, gy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<int> fie[1000006];
vector<ll> dp[4][1000006];

int main(void) {
  scanf("%d%d%d", &h, &w, &k);
  scanf("%d%d%d%d", &sy, &sx, &gy, &gx);
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 4; k++) {
        dp[k][i].push_back(1LL << 60);
      }
      if (s[j] == '.') {
        fie[i].push_back(0);
      } else {
        fie[i].push_back(-1);
      }
    }
  }
  dp[0][sy][sx] = 0;
  priority_queue<state> que;
  que.push(state(sx, sy, 0, 0));
  while (que.size()) {
    state st = que.top();
    que.pop();
    if (dp[st.d][st.y][st.x] < st.c) {
      continue;
    }
    // printf("%d %d %d %lld\n",st.x,st.y,st.d,st.c);
    for (int j = 0; j < 4; j++) {
      int nx = st.x + dx[j];
      int ny = st.y + dy[j];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (fie[ny][nx] == -1)
        continue;
      ll nc = st.c + 1;
      if (st.d != j) {
        if (st.c % k == 0) {
          nc = st.c + 1;
        } else {
          nc = st.c + k - (st.c % k) + 1;
        }
      }
      if (dp[j][ny][nx] > nc) {
        dp[j][ny][nx] = nc;
        que.push(state(nx, ny, j, nc));
      }
    }
  }
  ll ans = 1LL << 60;
  for (int i = 0; i < 4; i++) {
    ans = min(ans, dp[i][gy][gx]);
  }
  printf("%lld\n", ans == (1LL << 60) ? -1 : (ans + k - 1) / k);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

class state {
public:
  int x, y, d;
  ll c;
  state() {}
  state(int xx, int yy, int dd, ll cc) {
    x = xx;
    y = yy;
    d = dd;
    c = cc;
  }
  bool operator<(const state &s) const { return c > s.c; }
};

int h, w, k;
int sx, sy, gx, gy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<int> fie[1000006];
vector<ll> dp[4][1000006];

int main(void) {
  scanf("%d%d%d", &h, &w, &k);
  scanf("%d%d%d%d", &sy, &sx, &gy, &gx);
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 4; k++) {
        dp[k][i].push_back(1LL << 60);
      }
      if (s[j] == '.') {
        fie[i].push_back(0);
      } else {
        fie[i].push_back(-1);
      }
    }
  }
  dp[0][sy][sx] = 0;
  priority_queue<state> que;
  que.push(state(sx, sy, 0, 0));
  while (que.size()) {
    state st = que.top();
    que.pop();
    if (dp[st.d][st.y][st.x] < st.c) {
      continue;
    }
    // printf("%d %d %d %lld\n",st.x,st.y,st.d,st.c);
    for (int j = 0; j < 4; j++) {
      int nx = st.x + dx[j];
      int ny = st.y + dy[j];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (fie[ny][nx] == -1)
        continue;
      ll nc = st.c + 1;
      if (st.d != j) {
        if (st.c % k == 0) {
          nc = st.c + 1;
        } else {
          nc = st.c + k - (st.c % k) + 1;
        }
      }
      if (dp[j][ny][nx] > nc) {
        dp[j][ny][nx] = nc;
        que.push(state(nx, ny, j, nc));
      }
    }
  }
  ll ans = 1LL << 60;
  for (int i = 0; i < 4; i++) {
    ans = min(ans, dp[i][gy][gx]);
  }
  printf("%lld\n", ans == (1LL << 60) ? -1 : (ans + k - 1) / k);
  return 0;
}