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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (int)(n)-1; i >= (a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os << "]";
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << " " << p.second << ")";
}

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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;

using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

/**
 * @brief
 * 単一始点最短路(ダイクストラ)
 * 二分ヒープ(priority_queue)を使ってO((E+V)logV)
 * @author ?
 * @date 2019/12
 *
 * @param[in] g グラフ
 * @param[in] s 始点
 * @param[in] inf 到達不可能を表す無限値
 * @return vector<T> sからそれぞれの頂点への最短路
 *
 * @details
 * 2020/04/07 コメント追加、テスト有無のチェック by Md
 * 2020/05/04 inf に使う値を明示的に渡す. by haraba-k
 */

int main() {

  int H, W, K;
  cin >> H >> W >> K;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;

  vector<string> c(H);
  REP(i, H) cin >> c[i];

  using vvvi = vector<vvi>;
  vvvi dist(4, vvi(H, vi(W, INF)));

  using T = tuple<int, int, int, int>;
  priority_queue<T, vector<T>, greater<>> que;
  REP(l, 4) {
    dist[l][x1][y1] = 0;
    que.emplace(0, l, x1, y1);
  }

  int dirx[] = {0, 1, 0, -1}, diry[] = {1, 0, -1, 0};

  while (!que.empty()) {
    int d, l, x, y;
    tie(d, l, x, y) = que.top();
    que.pop();
    if (dist[l][x][y] < d)
      continue;

    // 方向転換
    REP(l1, 4) {
      int alt = dist[l][x][y];
      if (alt % K)
        alt += K - alt % K;
      if (dist[l1][x][y] > alt) {
        dist[l1][x][y] = alt;
        que.emplace(alt, l1, x, y);
      }
    }

    // l方向に1マス進む
    int nx = x + dirx[l], ny = y + diry[l];
    if (nx < 0 or ny < 0 or nx >= H or ny >= W or c[nx][ny] == '@')
      continue;
    if (dist[l][nx][ny] > dist[l][x][y] + 1) {
      dist[l][nx][ny] = dist[l][x][y] + 1;
      que.emplace(dist[l][nx][ny], l, nx, ny);
    }
  }

  int ans = INF;
  REP(l, 4) {
    if (dist[l][x2][y2] != INF)
      chmin(ans, (dist[l][x2][y2] + K - 1) / K);
  }

  cout << (ans == INF ? -1 : ans) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (int)(n)-1; i >= (a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os << "]";
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << " " << p.second << ")";
}

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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

int main() {

  int H, W, K;
  cin >> H >> W >> K;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;

  vector<string> c(H);
  REP(i, H) cin >> c[i];

  using T = tuple<int, int, int>;
  priority_queue<T, vector<T>, greater<T>> que;
  que.emplace(0, x1, y1);

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[x1][y1] = 0;

  const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

  while (!que.empty()) {
    int d, x, y;
    tie(d, x, y) = que.top();
    que.pop();
    if (x == x2 and y == y2)
      break;
    if (dist[x][y] < d)
      continue;
    REP(l, 4) {
      FOR(k, 1, max(W, H)) {
        int nx = x + k * dx[l], ny = y + k * dy[l];
        if (nx < 0 or ny < 0 or nx >= H or ny >= W or c[nx][ny] == '@')
          break;
        int alt = dist[x][y] + (k + K - 1) / K;
        if (dist[nx][ny] > alt) {
          dist[nx][ny] = alt;
          que.emplace(alt, nx, ny);
        }
      }
    }
  }

  cout << (dist[x2][y2] == INF ? -1 : dist[x2][y2]) << endl;

  return 0;
}