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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
long double eps = 1e-9;
long double pi = acos(-1);

ll mod = 1e9 + 7;
template <typename T> void add(T &a, T b) { a = (a + b) % mod; }
template <typename T> void sub(T &a, T b) { a = (a + mod - b) % mod; }

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int h, w, a;
  cin >> h >> w >> a;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  s[sx][sy] = 'S';
  s[gx][gy] = 'G';
  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9;
    }
  }
  queue<array<int, 5>> q;
  q.push({0, sx, sy, -1, 0});
  while (q.size()) {
    array<int, 5> p = q.front();
    q.pop();
    int x = p[1], y = p[2], dist = p[0];
    int r = p[3], ok = (p[4] == a);
    int ndist = dist + 1;
    if (d[x][y] < dist)
      continue;
    for (int i = 0; i < 4; i++) {
      if (i + r == 3)
        continue;
      if (i == r && !ok)
        continue;
      for (int k = 1; k <= a; k++) {
        int nx = x + k * dx[i];
        int ny = y + k * dy[i];
        if (nx >= h || ny >= w)
          break;
        if (nx >= 0 && ny >= 0) {
          if (s[nx][ny] == '@')
            break;
          if (d[nx][ny] <= ndist)
            continue;
          chmin(d[nx][ny], ndist);
          if (s[nx][ny] == 'G') {
            cout << dist + 1 << endl;
            return 0;
          }
          q.push({ndist, nx, ny, i, k});
        }
      }
    }
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
long double eps = 1e-9;
long double pi = acos(-1);

ll mod = 1e9 + 7;
template <typename T> void add(T &a, T b) { a = (a + b) % mod; }
template <typename T> void sub(T &a, T b) { a = (a + mod - b) % mod; }

class Dijkstra {
public:
  struct edge {
    ll v, dist;
  };

  struct state {
    ll v, cost;
    bool operator>(const state s) const { return cost > s.cost; }
  };

  const ll INF = (1LL << 60);
  ll N;
  ll MOD = 1e9 + 7;
  vector<vector<edge>> E;

  Dijkstra(ll n) : N(n), E(n) {}

  // 有効グラフの時はこっち。u→vに距離dで結ぶ
  void add_directed_edge(ll u, ll v, ll d) { E[u].push_back((edge){v, d}); }

  // 無向グラフの時はこっち。uとvを双方向に距離dで結ぶ
  void add_undirected_edge(ll u, ll v, ll d) {
    E[u].push_back((edge){v, d});
    E[v].push_back((edge){u, d});
  }

  // Sを始点として、他の頂点への最短経路を探す
  vector<long long> shortest_path(long long S) {
    vector<long long> dp(E.size(), INF);
    priority_queue<state, vector<state>, greater<state>> q;
    q.push((state){S, 0});
    while (!q.empty()) {
      long long v = q.top().v, cost = q.top().cost;
      q.pop();
      if (dp[v] <= cost)
        continue;
      dp[v] = cost;
      for (int i = 0; i < E[v].size(); i++) {
        long long nv = E[v][i].v, ncost = cost + E[v][i].dist;
        if (dp[nv] > ncost)
          q.push((state){nv, ncost});
      }
    }
    return dp;
  }

  // Sを始点として、他の頂点への最短経路と経路数のを調べる
  vector<pair<ll, ll>> path_count(ll S) {
    vector<pair<ll, ll>> dp(E.size(), {INF, 0});
    priority_queue<state, vector<state>, greater<state>> q;
    q.push((state){S, 0});
    dp[S].first = 0;
    dp[S].second = 1;
    while (!q.empty()) {
      ll v = q.top().v, cost = q.top().cost;
      q.pop();
      if (dp[v].first < cost)
        continue;
      for (int i = 0; i < E[v].size(); i++) {
        ll nv = E[v][i].v, ncost = cost + E[v][i].dist;
        if (dp[nv].first > ncost) {
          dp[nv].first = ncost;
          dp[nv].second = dp[v].second;
          q.push((state){nv, ncost});
        } else if (dp[nv].first == ncost) {
          dp[nv].second += dp[v].second;
          dp[nv].second %= MOD;
        }
      }
    }
    return dp;
  }
};

class UnionFind {
public:
  // 親の番号を格納する。親だった場合は-(その集合のサイズ)
  vector<int> parent;

  UnionFind(int N) { parent = vector<int>(N, -1); }

  int root(int A) {
    if (parent[A] < 0)
      return A;
    return parent[A] = root(parent[A]);
  }

  int size(int A) { return -parent[root(A)]; }

  bool unite(int A, int B) {
    A = root(A), B = root(B);
    if (A == B)
      return false;

    if (size(A) < size(B))
      swap(A, B);
    parent[A] += parent[B];
    parent[B] = A;
    return true;
  }

  bool same(int A, int B) { return root(A) == root(B); }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int h, w, a;
  cin >> h >> w >> a;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  s[sx][sy] = 'S';
  s[gx][gy] = 'G';
  Dijkstra dij(h * w);
  UnionFind uni(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '@')
        continue;
      for (int k = 0; k < 4; k++) {
        int x = i + dx[k];
        int y = j + dy[k];
        if (x >= 0 && y >= 0 && x < h && y < w) {
          if (s[x][y] == '@')
            continue;
          uni.unite(i * w + j, x * w + y);
        }
      }
    }
  }
  if (uni.same(sx * w + sy, gx * w + gy) == false) {
    cout << -1 << endl;
    return 0;
  }
  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '@')
        continue;
      for (int k = 1; k <= a; k++) {
        int x = i + k;
        if (x >= h)
          break;
        if (s[x][j] == '@')
          break;
        dij.add_undirected_edge(i * w + j, x * w + j, 1);
      }
      for (int k = 1; k <= a; k++) {
        int y = j + k;
        if (y >= w)
          break;
        if (s[i][y] == '@')
          break;
        dij.add_undirected_edge(i * w + j, i * w + y, 1);
      }
    }
  }
  cout << dij.shortest_path(sx * w + sy)[gx * w + gy] << endl;
}