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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define __                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define forn(i, a, n) for (int i = a; i < n; i++)
typedef long long int lli;
typedef long double Double;
typedef pair<int, int> pii;
typedef pair<pii, int> iii;
typedef vector<int> vi;
typedef vector<vi> vvi;
vector<pii> mov = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const Double inf = 100000000.0;
int main() {
  __ int n, m, k;
  cin >> n >> m >> k;
  int xs, ys, xf, yf;
  cin >> xs >> ys >> xf >> yf;
  vector<vector<bool>> a(n + 1, vector<bool>(m + 1));
  vector<vector<vector<Double>>> dist(
      n + 1, vector<vector<Double>>(m + 1, vector<Double>(2, inf)));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == '.')
        a[i][j] = true;
    }
  }
  Double div = 1.0 / Double(k);
  dist[xs][ys][0] = dist[xs][ys][1] = 0.0;
  priority_queue<pair<Double, iii>> q;
  for (auto e : mov) {
    int f = (e.se != 0) ? 1 : 0;
    int x = xs + e.fi, y = ys + e.se;
    if (x >= 1 && x <= n && y >= 1 && y <= m) {
      if (a[x][y]) {
        dist[x][y][f] = div;
        q.push(pair<Double, iii>(-dist[x][y][f], iii(pii(x, y), f)));
      }
    }
  }
  // meto og
  // f = 1 de izq a der o der a izq
  //  al cambio de direccion viene el ceil
  while (!q.empty()) {
    int xa = q.top().se.fi.fi;
    int ya = q.top().se.fi.se;
    Double d = -q.top().fi;
    int of = q.top().se.se;
    q.pop();
    if (d != dist[xa][ya][of])
      continue;
    // movi
    for (auto e : mov) {
      int x = xa + e.fi, y = ya + e.se;
      int f = (e.se != 0) ? 1 : 0;
      if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y]) {
        Double currd = dist[xa][ya][of];
        if (f != of)
          currd = ceil(currd);
        if (currd + div < dist[x][y][f]) {
          dist[x][y][f] = currd + div;
          q.push(pair<Double, iii>(-dist[x][y][f], iii(pii(x, y), f)));
        }
      }
    }
  }
  /*
  cout << "distancias\\n";
  for(int i=1; i <=n; i++){
          for(int j=1; j <=m; j++){
                  if(dist[i][j]== inf){
                          cout << "oooo ";
                          continue;
                  }
                  cout << fixed << setprecision(4) << dist[i][j] << " ";
          }
          cout << endl;
  }
  */
  // al final ceil al resultado
  Double mini = min(dist[xf][yf][0], dist[xf][yf][1]);
  if (mini == inf) {
    cout << -1 << endl;
  } else {
    cout << ceil(mini) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define __                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define forn(i, a, n) for (int i = a; i < n; i++)
typedef long long int lli;
typedef long double Double;
typedef pair<int, int> pii;
typedef pair<pii, int> iii;
typedef vector<int> vi;
typedef vector<vi> vvi;
vector<pii> mov = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const Double inf = 100000000.0;
int main() {
  __ int n, m, k;
  cin >> n >> m >> k;
  int xs, ys, xf, yf;
  cin >> xs >> ys >> xf >> yf;
  vector<vector<bool>> a(n + 1, vector<bool>(m + 1));
  vector<vector<vector<Double>>> dist(
      n + 1, vector<vector<Double>>(m + 1, vector<Double>(2, inf)));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == '.')
        a[i][j] = true;
    }
  }
  Double div = 1.0 / Double(k);
  dist[xs][ys][0] = dist[xs][ys][1] = 0.0;
  priority_queue<pair<Double, iii>> q;
  for (auto e : mov) {
    int f = (e.se != 0) ? 1 : 0;
    int x = xs + e.fi, y = ys + e.se;
    if (x >= 1 && x <= n && y >= 1 && y <= m) {
      if (a[x][y]) {
        dist[x][y][f] = div;
        q.push(pair<Double, iii>(-dist[x][y][f], iii(pii(x, y), f)));
      }
    }
  }
  // meto og
  // f = 1 de izq a der o der a izq
  //  al cambio de direccion viene el ceil
  while (!q.empty()) {
    int xa = q.top().se.fi.fi;
    int ya = q.top().se.fi.se;
    Double d = -q.top().fi;
    int of = q.top().se.se;
    q.pop();
    if (d != dist[xa][ya][of])
      continue;
    // movi
    for (auto e : mov) {
      int x = xa + e.fi, y = ya + e.se;
      int f = (e.se != 0) ? 1 : 0;
      if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y]) {
        Double currd = dist[xa][ya][of];
        if (f != of)
          currd = ceil(currd);
        if (currd + div <= dist[x][y][f]) {
          dist[x][y][f] = currd + div;
          q.push(pair<Double, iii>(-dist[x][y][f], iii(pii(x, y), f)));
        }
      }
    }
  }
  /*
  cout << "distancias\\n";
  for(int i=1; i <=n; i++){
          for(int j=1; j <=m; j++){
                  if(dist[i][j]== inf){
                          cout << "oooo ";
                          continue;
                  }
                  cout << fixed << setprecision(4) << dist[i][j] << " ";
          }
          cout << endl;
  }
  */
  // al final ceil al resultado
  Double mini = min(dist[xf][yf][0], dist[xf][yf][1]);
  if (mini == inf) {
    cout << -1 << endl;
  } else {
    cout << ceil(mini) << endl;
  }
  return 0;
}