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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, a, b) for (int i = (a); i < (b); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using tl = tuple<ll, ll, ll>;

template <typename T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using tf = tuple<ll, ll, ll, ll, ll>;

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> grid(h);
  cin >> grid;

  if (grid[x1][y1] == '@') {
    cout << -1 << endl;
    return 0;
  }

  vector<LP> dir{
      LP(1, 0),
      LP(0, 1),
      LP(-1, 0),
      LP(0, -1),
  };

  priority_queue<tf, vector<tf>, greater<tf>> q;
  rep(i, 4) q.emplace(0, 0, x1, y1, i);
  vector<vvl> dist(h, vvl(w, vl(4, LINF)));
  while (!q.empty()) {
    tf p = q.top();
    q.pop();
    ll dn = get<0>(p);
    ll dd = get<1>(p);
    ll x = get<2>(p);
    ll y = get<3>(p);
    ll di = get<4>(p);
    if (dist[x][y][di] != LINF)
      continue;

    dist[x][y][di] = dn + (dd > 0);
    rep(i, 4) {
      LP nd = dir[i];
      ll nx = x + nd.fi;
      ll ny = y + nd.se;
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || grid[nx][ny] == '@')
        continue;
      ll ndn = di == i ? dn + (dd + 1) / k : dn + (dd > 0);
      ll ndd = di == i ? (dd + 1) % k : 1;
      q.emplace(ndn, ndd, nx, ny, i);
    }
  }

  ll ans = LINF;
  rep(i, 4) chmin(ans, dist[x2][y2][i]);
  if (ans == LINF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, a, b) for (int i = (a); i < (b); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using tl = tuple<ll, ll, ll>;

template <typename T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> grid(h);
  cin >> grid;

  if (grid[x1][y1] == '@') {
    cout << -1 << endl;
    return 0;
  }

  vector<LP> dir{
      LP(1, 0),
      LP(0, 1),
      LP(-1, 0),
      LP(0, -1),
  };

  priority_queue<vl, vvl, greater<vl>> q;
  rep(i, 4) q.push(vl{0, 0, x1, y1, i});
  vector<vvl> dist(h, vvl(w, vl(4, LINF)));
  while (!q.empty()) {
    vl p = q.top();
    q.pop();
    ll dn = p[0];
    ll dd = p[1];
    ll x = p[2];
    ll y = p[3];
    ll di = p[4];
    if (dist[x][y][di] != LINF)
      continue;

    dist[x][y][di] = dn + (dd > 0);
    rep(i, 4) {
      LP nd = dir[i];
      ll nx = x + nd.fi;
      ll ny = y + nd.se;
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || grid[nx][ny] == '@')
        continue;
      ll ndn = di == i ? dn + (dd + 1) / k : dn + (dd > 0);
      ll ndd = di == i ? (dd + 1) % k : 1;
      q.push(vl{ndn, ndd, nx, ny, i});
    }
  }

  ll ans = LINF;
  rep(i, 4) chmin(ans, dist[x2][y2][i]);
  if (ans == LINF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}