Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RNG(i, from, to) for (ll i = (from); i < (ll)(to); i++)
#define gcd(i, j) __gcd((i), (j))
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vecll;
template <typename S, typename T> string to_string(pair<S, T> p) {
  return "(" + to_string(p.first) + +to_string(p.second) + ")";
}
template <typename T> void println(T e) { cout << to_string(e) << endl; }
template <typename T> void println(const vector<T> &v) {
  cout << "[";
  for (const auto &e : v) {
    cout << to_string(e) << ",";
  }
  cout << "]";
  cout << endl;
}
template <typename T> void println(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    println(v);
  }
}
template <typename Iter> void println(Iter first, Iter last) {
  for (auto it = first; it != last; it++) {
    cout << *it << " ";
  };
  cout << endl;
}
template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}
template <typename T> T mod_inv(T x, const T &p) {
  return mod_pow(x, p - 2, p);
}
const ll DVSR = 1e9 + 7;
// pair<int, int> Pii(int i, int j) { return pair<int, int>(i, j); }
// pair<ll, ll> Pll(ll i, ll j) { return pair<ll, ll>(i, j); }

typedef tuple<ll, ll, ll, ll> sc_t;

class Compare {
public:
  bool operator()(const sc_t &a, const sc_t &b) const {
    if (get<0>(a) > get<0>(b)) {
      return true;
    } else if (get<0>(a) < get<0>(b)) {
      return false;
    } else {
      return get<1>(a) > get<1>(b);
    }
  }
};

ll H, W, K;
ll Si, Sj, Gi, Gj;
vector<string> MP;
vector<vector<ll>> RES;
vector<vector<vector<ll>>> RES2;

priority_queue<sc_t, vector<sc_t>, Compare> PQ;
ll dis[] = {1, 0, -1, 0};
ll djs[] = {0, 1, 0, -1};

void solve() {
  while (PQ.size()) {
    sc_t sc = PQ.top();
    PQ.pop();
    ll score = get<0>(sc);
    ll dir = get<1>(sc);
    ll i = get<2>(sc);
    ll j = get<3>(sc);

    RES[i][j] = min(RES[i][j], score);
    if (dir >= 0)
      RES2[i][j][dir] = min(RES2[i][j][dir], score);
    // if (RES[i][j] < score) continue;
    REP(di, 4) {
      ll ni = i + dis[di];
      ll nj = j + djs[di];
      if (ni >= 0 && ni < H && nj >= 0 && nj < W && MP[ni][nj] == '.') {
        auto next_sc = score;
        if (dir != di && dir != -1 && (next_sc % K)) {
          next_sc -= (next_sc % K);
          next_sc += K;
        }
        next_sc += 1;

        if (next_sc < RES[ni][nj]) {
          RES[ni][nj] = min(RES[ni][nj], next_sc);
          RES2[ni][nj][di] = min(RES2[ni][nj][di], next_sc);
          PQ.push(sc_t(next_sc, di, ni, nj));
        } else if (next_sc < RES2[ni][nj][di]) {
          RES2[ni][nj][di] = min(RES2[ni][nj][di], next_sc);
          PQ.push(sc_t(next_sc, di, ni, nj));
        }
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  cin >> H >> W >> K;
  cin >> Si >> Sj >> Gi >> Gj;
  Si--;
  Sj--;
  Gi--;
  Gj--;

  ll INF = 1000000000LL * 1000000000LL;
  MP.resize(H);
  RES.assign(H, vector<ll>(W, INF));
  RES2.assign(H, vector<vector<ll>>(W, vector<ll>{INF, INF, INF, INF}));
  REP(i, H) cin >> MP[i];
  PQ.push(sc_t(0, -1, Si, Sj));
  RES[Si][Sj] = 0;
  RES2[Si][Sj][0] = 0;
  RES2[Si][Sj][1] = 0;
  RES2[Si][Sj][2] = 0;
  RES2[Si][Sj][3] = 0;
  solve();
  // for (const auto& s: RES) println(s);
  if (RES[Gi][Gj] == INF) {
    cout << -1 << endl;
  } else {
    cout << RES[Gi][Gj] / K + (RES[Gi][Gj] % K > 0) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RNG(i, from, to) for (ll i = (from); i < (ll)(to); i++)
#define gcd(i, j) __gcd((i), (j))
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vecll;
template <typename S, typename T> string to_string(pair<S, T> p) {
  return "(" + to_string(p.first) + +to_string(p.second) + ")";
}
template <typename T> void println(T e) { cout << to_string(e) << endl; }
template <typename T> void println(const vector<T> &v) {
  cout << "[";
  for (const auto &e : v) {
    cout << to_string(e) << ",";
  }
  cout << "]";
  cout << endl;
}
template <typename T> void println(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    println(v);
  }
}
template <typename Iter> void println(Iter first, Iter last) {
  for (auto it = first; it != last; it++) {
    cout << *it << " ";
  };
  cout << endl;
}
template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}
template <typename T> T mod_inv(T x, const T &p) {
  return mod_pow(x, p - 2, p);
}
const ll DVSR = 1e9 + 7;
// pair<int, int> Pii(int i, int j) { return pair<int, int>(i, j); }
// pair<ll, ll> Pll(ll i, ll j) { return pair<ll, ll>(i, j); }

typedef tuple<ll, ll, char, ll, ll> sc_t;

class Compare {
public:
  bool operator()(const sc_t &a, const sc_t &b) const {
    if (get<0>(a) > get<0>(b)) {
      return true;
    } else if (get<0>(a) < get<0>(b)) {
      return false;
    } else {
      return get<1>(a) > get<1>(b);
    }
  }
};

// bool msmll(const pll& i, const pll& j) {
//   if (i.first < j.first) {
//     return true;
//   } else if (i.first > j.first) {
//     return false;
//   } else {
//     if (i.second <= j.second) {
//       return true;
//     } else {
//       return false;
//     }
//   }
// }

// pll mpll(const pll& i, const pll& j) {
//   if (msmll(i, j)) return i;
//   else return j;
// }

ll H, W, K;
double K_inv;
ll Si, Sj, Gi, Gj;
vector<string> MP;
vector<vector<pll>> RES;
priority_queue<sc_t, vector<sc_t>, Compare> PQ;
vector<vector<vector<ll>>> visited;
ll dis[] = {1, 0, -1, 0};
ll djs[] = {0, 1, 0, -1};
ll ddd[] = {1, 2, 3, 4};

void solve() {
  while (PQ.size()) {
    sc_t sc = PQ.top();
    PQ.pop();
    ll score = get<0>(sc);
    ll rem = get<1>(sc);
    ll dir = get<2>(sc);
    ll i = get<3>(sc);
    ll j = get<4>(sc);
    RES[i][j] = RES[i][j] > pll(score, rem) ? pll(score, rem) : RES[i][j];
    REP(l, 4) {
      ll ni = i + dis[l];
      ll nj = j + djs[l];
      ll di = ddd[l];
      if (ni >= 0 && ni < H && nj >= 0 && nj < W && !visited[ni][nj][di] &&
          MP[ni][nj] == '.') {
        auto next_sc = score;
        auto next_rem = rem;
        if (dir != di && dir != 0 && next_rem) {
          next_sc += 1;
          next_rem = 0;
        }
        next_rem += 1;
        next_sc += (next_rem / K);
        next_rem %= K;
        visited[i][j][dir] = 1;
        if (pll(next_sc, next_rem) < RES[ni][nj]) {
          PQ.push(sc_t(next_sc, next_rem, di, ni, nj));
        }
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  cin >> H >> W >> K;
  K_inv = 1.0 / K;
  cin >> Si >> Sj >> Gi >> Gj;
  Si--;
  Sj--;
  Gi--;
  Gj--;

  MP.resize(H);
  RES.assign(H, vector<pll>(W, pll(10e15, 0)));
  visited.assign(H, vector<vector<ll>>(W, {0, 0, 0, 0}));
  REP(i, H) cin >> MP[i];
  PQ.push(sc_t(0, 0, 0, Si, Sj));
  solve();

  // for (const auto& s: RES) println(s);
  if (RES[Gi][Gj].first == (ll)10e15) {
    cout << -1 << endl;
  } else {
    // cout << RES[Gi][Gj] << endl;
    cout << RES[Gi][Gj].first + (RES[Gi][Gj].second > 0) << endl;
  }
  // cout << K_inv << endl;
  return 0;
}