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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define MOD 1000000007
#define MOD2 998244353
#define int long long
#define double long double
#define EPS 1e-9
// #define PI 3.14159265358979

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) {
  for (int i = 0; i < A.size(); i++)
    os << A[i] << " ";
  os << endl;
  return os;
}
template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) {
  int N = A.size();
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < A[i].size(); j++)
      os << A[i][j] << " ";
    os << endl;
  }
  return os;
}

typedef pair<int, int> pii;
typedef long long ll;

struct edge {
  int from, to, d, c;
  edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
    from = _from;
    to = _to;
    d = _d;
    c = _c;
  }
  bool operator<(const edge &rhs) const {
    return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
  }
};
struct aabb {
  int x1, y1, x2, y2;
  aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};

typedef vector<edge> edges;
typedef vector<edges> graph;
struct flow {
  int to, cap, rev, cost;
  flow(int to = 0, int cap = 0, int rev = 0, int cost = 0)
      : to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector<vector<flow>> flows;

const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);

int pow2(int n) { return 1LL << n; }
template <typename T, typename U> bool chmin(T &x, const U &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T, typename U> bool chmax(T &x, const U &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

struct initializer {
  initializer() { cout << fixed << setprecision(20); }
};
initializer _____;
vector<int> dijkstra(graph &G, int s) {
  /// stat must have "<"&">"
  /// init d=vector<stat>
  vector<int> d(G.size(), LINF);
  d[s] = 0;
  // init q=p-queue<pair<stat,index> >
  priority_queue<pii, vector<pii>, greater<pii>> q;
  q.push(pii(0, s));

  while (!q.empty()) {
    pii p = q.top();
    q.pop();
    if (d[p.second] < p.first)
      continue;
    for (int i = 0; i < G[p.second].size(); i++) {
      edge e = G[p.second][i];
      if (p.first + e.d < d[e.to]) {
        d[e.to] = p.first + e.d;
        q.push(pii(d[e.to], e.to));
      }
    }
  }
  return d;
}
int N, M, K, T, Q, H, W;
signed main() {
  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];
  vector<vector<int>> grid(H, vector<int>(W, 0));
  rep(i, H) rep(j, W) if (C[i][j] == '@') grid[i][j] = 1;
  vector<vector<vector<pii>>> d(
      H, vector<vector<pii>>(W, vector<pii>(4, pii(LINF, LINF))));
  d[x1][y1][0] = pii(0, 0);
  priority_queue<pair<pii, pair<pii, int>>, vector<pair<pii, pair<pii, int>>>,
                 greater<pair<pii, pair<pii, int>>>>
      q;
  pair<pii, pair<pii, int>> s;
  s.first = pii(0, 0);
  s.second.first = pii(x1, y1);
  s.second.second = 0;
  q.push(s);
  while (!q.empty()) {
    pair<pii, pair<pii, int>> p = q.top();
    q.pop();
    int i = p.second.first.first;
    int j = p.second.first.second;
    int k = p.second.second;
    pii dist = p.first;
    if (d[i][j][k] < dist)
      continue;
    int ni = i + di[k];
    int nj = j + dj[k];
    if (0 <= ni && ni < H && 0 <= nj && nj < W && (!grid[ni][nj])) {
      pii ndist = dist;
      ndist.second++;
      if (ndist.second >= K) {
        ndist.second -= K;
        ndist.first++;
      }
      if (ndist < d[ni][nj][k]) {
        d[ni][nj][k] = ndist;
        q.push(make_pair(ndist, pair<pii, int>(pii(ni, nj), k)));
      }
    }
    int nk = k;
    pii ndist = dist;
    if (ndist.second > 0) {
      ndist.first++;
      ndist.second = 0;
    }
    rep(_, 3) {
      nk = (nk + 1) % 4;
      if (ndist < d[i][j][nk]) {
        d[i][j][nk] = ndist;
        q.push(make_pair(ndist, pair<pii, int>(pii(i, j), nk)));
      }
    }
  }
  pii ans = pii(LINF, LINF);
  rep(k, 4) { chmin(ans, d[x2][y2][k]); }
  if (ans == pii(LINF, LINF)) {
    cout << -1 << endl;
  } else {
    if (ans.second > 0)
      ans.first++;
    cout << ans.first << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define MOD 1000000007
#define MOD2 998244353
#define int long long
#define double long double
#define EPS 1e-9
// #define PI 3.14159265358979

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) {
  for (int i = 0; i < A.size(); i++)
    os << A[i] << " ";
  os << endl;
  return os;
}
template <> ostream &operator<<(ostream &os, const vector<vector<int>> &A) {
  int N = A.size();
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < A[i].size(); j++)
      os << A[i][j] << " ";
    os << endl;
  }
  return os;
}

typedef pair<int, int> pii;
typedef long long ll;

struct edge {
  int from, to, d, c;
  edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
    from = _from;
    to = _to;
    d = _d;
    c = _c;
  }
  bool operator<(const edge &rhs) const {
    return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
  }
};
struct aabb {
  int x1, y1, x2, y2;
  aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};

typedef vector<edge> edges;
typedef vector<edges> graph;
struct flow {
  int to, cap, rev, cost;
  flow(int to = 0, int cap = 0, int rev = 0, int cost = 0)
      : to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector<vector<flow>> flows;

const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);

int pow2(int n) { return 1LL << n; }
template <typename T, typename U> bool chmin(T &x, const U &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T, typename U> bool chmax(T &x, const U &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

struct initializer {
  initializer() { cout << fixed << setprecision(20); }
};
initializer _____;

int N, M, K, T, Q, H, W;
signed main() {
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  vector<string> grid2(H);
  rep(i, H) cin >> grid2[i];
  vector<vector<int>> grid(H, vector<int>(W, 0));
  rep(i, H) rep(j, W) if (grid2[i][j] == '@') grid[i][j] = 1;

  vector<vector<int>> dp(H, vector<int>(W, LINF));
  queue<pii> q;
  dp[x1][y1] = 0;
  q.push(pii(x1, y1));
  while (!q.empty()) {
    pii p = q.front();
    int i = p.first;
    int j = p.second;
    q.pop();
    rep(k, 4) {
      int ni = i + di[k];
      int nj = j + dj[k];
      rep(_, K) {
        if (0 <= ni && ni < H && 0 <= nj && nj < W) {
          if (grid[ni][nj] || dp[ni][nj] != LINF && dp[ni][nj] > dp[i][j] + 1)
            break;
          if (dp[i][j] + 1 < dp[ni][nj]) {
            dp[ni][nj] = dp[i][j] + 1;
            q.push(pii(ni, nj));
          }
        }
        ni += di[k];
        nj += dj[k];
      }
    }
  }
  // cout << dp;
  if (dp[x2][y2] == LINF) {
    cout << -1 << endl;
  } else {
    cout << dp[x2][y2] << endl;
  }
  return 0;
}