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

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

struct Elem {
  int x, y, d, dist, straight;
  Elem(int x_, int y_, int d_, int dist_, int s)
      : x(x_), y(y_), d(d_), dist(dist_), straight(s) {}
  bool operator<(const Elem &e) const {
    if (dist != e.dist)
      return dist > e.dist;
    return straight > e.straight;
  }
};

int main() {
  int H, W, K;
  scanf("%d%d%d", &H, &W, &K);
  int sx, sy, gx, gy;
  scanf("%d%d%d%d", &sx, &sy, &gx, &gy);
  sx--, sy--, gx--, gy--;

  vector<string> board(H);
  for (int i = 0; i < H; i++) {
    cin >> board[i];
  }

  const pair<int, int> NIL(INF, INF);
  vector<vector<vector<pair<int, int>>>> ans(
      H, vector<vector<pair<int, int>>>(W, vector<pair<int, int>>(4, NIL)));

  priority_queue<Elem> que;
  for (int k = 0; k < 4; k++) {
    ans[sx][sy][k] = make_pair(0, 0);
    que.emplace(sx, sy, k, 0, 0);
  }
  while (que.size()) {
    // Elem e = que.front(); que.pop();
    Elem e = que.top();
    que.pop();
    int x = e.x, y = e.y, d = e.d, dist = e.dist, straight = e.straight;
    if (make_pair(dist, straight) > ans[x][y][d])
      continue;
    for (int nd = 0; nd < 4; nd++) {
      int nx = x + dx[nd], ny = y + dy[nd];
      if (nx < 0 or nx >= H or ny < 0 or ny >= W)
        continue;
      if (board[nx][ny] == '@')
        continue;

      int n_dist = dist, n_straight = straight + 1;
      if (d != nd or straight == K) {
        n_dist++;
        n_straight = 1;
      }
      if (make_pair(n_dist, n_straight) < ans[nx][ny][nd]) {
        ans[nx][ny][nd] = make_pair(n_dist, n_straight);
        que.emplace(nx, ny, nd, n_dist, n_straight);
      }
    }
  }

  int res = INF;
  for (int k = 0; k < 4; k++) {
    if (ans[gx][gy][k] == NIL)
      continue;
    int cand = ans[gx][gy][k].first;
    if (ans[gx][gy][k].second > 0)
      cand++;
    chmin(res, cand);
  }
  if (res == INF)
    puts("-1");
  else
    printf("%d\\n", res);
  return 0;
}


Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

struct Elem {
  int x, y, d, dist, straight;
  Elem(int x_, int y_, int d_, int dist_, int s)
      : x(x_), y(y_), d(d_), dist(dist_), straight(s) {}
  bool operator<(const Elem &e) const {
    if (dist != e.dist)
      return dist < e.dist;
    return straight < e.straight;
  }
};

int main() {
  int H, W, K;
  scanf("%d%d%d", &H, &W, &K);
  int sx, sy, gx, gy;
  scanf("%d%d%d%d", &sx, &sy, &gx, &gy);
  sx--, sy--, gx--, gy--;

  vector<string> board(H);
  for (int i = 0; i < H; i++) {
    cin >> board[i];
  }

  const pair<int, int> NIL(INF, INF);
  vector<vector<vector<pair<int, int>>>> ans(
      H, vector<vector<pair<int, int>>>(W, vector<pair<int, int>>(4, NIL)));

  queue<Elem> que;
  for (int k = 0; k < 4; k++) {
    ans[sx][sy][k] = make_pair(0, 0);
    que.emplace(sx, sy, k, 0, 0);
  }
  while (que.size()) {
    Elem e = que.front();
    que.pop();
    int x = e.x, y = e.y, d = e.d, dist = e.dist, straight = e.straight;
    for (int nd = 0; nd < 4; nd++) {
      int nx = x + dx[nd], ny = y + dy[nd];
      if (nx < 0 or nx >= H or ny < 0 or ny >= W)
        continue;
      if (board[nx][ny] == '@')
        continue;

      int n_dist = dist, n_straight = straight + 1;
      if (d != nd or straight == K) {
        n_dist++;
        n_straight = 1;
      }
      if (make_pair(n_dist, n_straight) < ans[nx][ny][nd]) {
        ans[nx][ny][nd] = make_pair(n_dist, n_straight);
        que.emplace(nx, ny, nd, n_dist, n_straight);
      }
    }
  }

  int res = INF;
  for (int k = 0; k < 4; k++) {
    if (ans[gx][gy][k] == NIL)
      continue;
    int cand = ans[gx][gy][k].first;
    if (ans[gx][gy][k].second > 0)
      cand++;
    chmin(res, cand);
  }
  if (res == INF)
    puts("-1");
  else
    printf("%d\\n", res);
  return 0;
}