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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

// #define int ll
#define double ld
#define loop(i, n) for (int i = 0; i < (int)n; ++i)
#define loop1(i, n) for (int i = 1; i <= (int)n; ++i)
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define all(x) begin(x), end(x)
#define ti tuple<int, int, int>
#define Point Vect
#define no                                                                     \\
  {                                                                            \\
    cout << 0;                                                                 \\
    return;                                                                    \\
  }
#define mkp make_pair
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr

const int INF = 1e9 + 7;

vector<vector<bool>> f, used;
vector<vector<pi>> d[4];
pi dx[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void dijkstra(int stx, int sty, int step) {
  priority_queue<pair<pi, pi>, vector<pair<pi, pi>>, greater<pair<pi, pi>>> q;
  loop(i, 4) {
    q.push({{0, step}, {stx, sty}});
    ;
    d[i][stx][sty] = {0, step};
  }
  while (!q.empty()) {
    auto v = q.top();
    q.pop();
    int x = v.S.F, y = v.S.S, cd = v.F.F, ck = v.F.S;
    if (used[x][y])
      continue;
    loop(i, 4) {
      int nx = x + dx[i].F, ny = y + dx[i].S;
      if (f[nx][ny])
        continue;
      pi add = (d[i][x][y].F > cd || d[i][x][y].S == step
                    ? mkp(cd + 1, 1)
                    : mkp(cd, d[i][x][y].S + 1));
      if (d[i][nx][ny] > add) {
        d[i][nx][ny] = add;
        q.push({add, {nx, ny}});
      }
    }
  }
}

void solve() {
  int h, w, step;
  cin >> h >> w >> step;
  int stx, sty, fx, fy;
  cin >> stx >> sty >> fx >> fy;
  f.resize(h + 2);
  used.resize(h + 2);
  loop(i, 4) d[i].resize(h + 2);
  loop(i, h + 2) {
    f[i].resize(w + 2);
    used[i].resize(w + 2);
    loop(j, 4) d[j][i].resize(w + 2);
  }
  loop(i, h + 2) f[i][0] = f[i][w + 1] = 1;
  loop(i, w + 2) f[0][i] = f[h + 1][i] = 1;
  loop(i, h + 2) loop(j, w + 2) loop(k, 4) d[k][i][j] = {INF, INF};
  loop1(i, h) {
    loop1(j, w) {
      char c;
      cin >> c;
      f[i][j] = (c == '@');
    }
  }
  dijkstra(stx, sty, step);
  int ans = INF;
  loop(i, 4) ans = min(ans, d[i][fx][fy].F);
  cout << (ans == INF ? -1 : ans);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // int t; cin >> t; loop(i, t)
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

// #define int ll
#define double ld
#define loop(i, n) for (int i = 0; i < (int)n; ++i)
#define loop1(i, n) for (int i = 1; i <= (int)n; ++i)
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define all(x) begin(x), end(x)
#define ti tuple<int, int, int>
#define Point Vect
#define no                                                                     \\
  {                                                                            \\
    cout << 0;                                                                 \\
    return;                                                                    \\
  }
#define mkp make_pair
#define cerr                                                                   \\
  if (0)                                                                       \\
  cerr

const int INF = 1e9 + 7;

vector<vector<bool>> f;
vector<vector<pi>> d[4];
pi dx[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void dijkstra(int stx, int sty, int step) {
  priority_queue<pair<pi, ti>, vector<pair<pi, ti>>, greater<pair<pi, ti>>> q;
  loop(i, 4) {
    q.push({{0, step}, make_tuple(stx, sty, i)});
    ;
    d[i][stx][sty] = {0, step};
  }
  while (!q.empty()) {
    auto v = q.top();
    q.pop();
    int x, y, dr, cd = v.F.F, ck = v.F.S;
    tie(x, y, dr) = v.S;
    if (cd > d[dr][x][y].F || ck > d[dr][x][y].S)
      continue;
    loop(i, 4) {
      int nx = x + dx[i].F, ny = y + dx[i].S;
      if (f[nx][ny])
        continue;
      bool pls = (i != dr || ck == step);
      if (d[i][nx][ny].F > cd + pls ||
          (d[i][nx][ny].F == cd + pls && d[i][nx][ny].F > (pls ? 1 : ck + 1))) {
        d[i][nx][ny] = {cd + pls, (pls ? 1 : ck + 1)};
        q.push({d[i][nx][ny], make_tuple(nx, ny, i)});
      }
    }
  }
}

void solve() {
  int h, w, step;
  cin >> h >> w >> step;
  int stx, sty, fx, fy;
  cin >> stx >> sty >> fx >> fy;
  f.resize(h + 2);
  loop(i, 4) d[i].resize(h + 2);
  loop(i, h + 2) {
    f[i].resize(w + 2);
    loop(j, 4) d[j][i].resize(w + 2);
  }
  loop(i, h + 2) f[i][0] = f[i][w + 1] = 1;
  loop(i, w + 2) f[0][i] = f[h + 1][i] = 1;
  loop(i, h + 2) loop(j, w + 2) loop(k, 4) d[k][i][j] = {INF, INF};
  loop1(i, h) {
    loop1(j, w) {
      char c;
      cin >> c;
      f[i][j] = (c == '@');
    }
  }
  dijkstra(stx, sty, step);
  int ans = INF;
  loop(i, 4) ans = min(ans, d[i][fx][fy].F);
  cout << (ans == INF ? -1 : ans);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // int t; cin >> t; loop(i, t)
  solve();
  return 0;
}