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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, n) for (lint i = 0; i < n; i++)
#define repr(i, n) for (lint i = n - 1; i >= 0; i--)
#define repi(i, ini, n) for (lint i = ini; i < n; i++)
#define repir(i, ini, n) for (lint i = n - 1; i >= ini; i--)
#define repb(i, start, end) for (lint i = start; i <= end; i++)
#define repbr(i, start, end) for (lint i = end; i >= start; i--)

#define bit(n) (1LL << (n))

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rg(v, ini, end) v.begin() + ini, v.begin() + end

#define ret return 0;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

using lint = long long;
using ulint = unsigned long long;
using ld = long double;
struct xy {
  lint x, y;
  xy() : x(0), y(0) {}
  xy(lint _x, lint _y) : x(_x), y(_y) {}
  xy operator+(const xy &p) const { return xy(x + p.x, y + p.y); }
  bool operator<(xy p) const {
    if (y == p.y)
      return x < p.x;
    return y < p.y;
  }
};
struct xyd {
  ld x, y;
  xyd() : x(0), y(0) {}
  xyd(long double _x, long double _y) : x(_x), y(_y) {}
};
using vec = vector<lint>;
using vecd = vector<ld>;
using vecs = vector<string>;
using vecp = vector<xy>;
template <class T> using vect = vector<T>;
class vec2 : public vector<vector<lint>> {
public:
  vec2() {}
  vec2(lint n) : vector(n) {}
  vec2(lint h, lint w) : vector(h, vector<lint>(w)) {}
  vec2(lint h, lint w, lint v) : vector(h, vector<lint>(w, v)) {}
};

template <class T> using priq = priority_queue<T>;
template <class T> using rpriq = priority_queue<T, vector<T>, greater<T>>;
template <class Key, class Val> using hashmap = unordered_map<Key, Val>;
template <class Key> using hashset = unordered_set<Key>;

mt19937 mtrand((random_device())());

const double pi = 3.141592653589793238462;
const lint intmax = 9223372036854775807;
const lint inf = 1100100100100100100LL;

constexpr lint div2(lint p, lint q) { return (p + q - 1) / q; }

#if (__cplusplus < 201703L)

lint gcd(lint a, lint b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }
  return a;
}

lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }

#endif

template <class T> struct nval {
  lint n; // counts
  T val;
  nval() : n(0){};
  nval(lint _n, T _val) : n(_n), val(_val){};
};

template <class It, class It2> auto spacel(It i, It2 end) {
  if (i + 1 == end) {
    return '\\n';
  } else {
    return ' ';
  }
}

ostream &setp(ostream &ost) {
  cout << setprecision(60) << fixed;
  return ost;
}

const ulint mod = 1000000007;
// const ulint mod = 998244353;
const ld eps = 0.0000001;

lint h, w, k;

struct graph {
  struct R {
    lint to, c;
    bool f;
  };

  lint v, e;

  vector<vector<R>> gr;

  graph(lint _v) : v(_v), e(0), gr(v) {}

  void add_e(lint from, lint to, lint cost, bool f = false) {
    gr[from].push_back({to, cost, f});
    e++;
  }

  auto dijkstra(lint start) {
    vector<lint> d(v, inf);

    d[start] = 0;

    struct Vc {
      lint pos, d;
      bool operator<(const Vc &p) const { return d > p.d; }
    };

    priority_queue<Vc> que;
    que.push({start, 0});

    while (!que.empty()) {
      auto vc = que.top();
      que.pop();

      rep(i, gr[vc.pos].size()) {
        lint nc = vc.d + gr[vc.pos][i].c;
        if (gr[vc.pos][i].f)
          nc = div2(nc, k) * k;
        if (nc < d[gr[vc.pos][i].to]) {
          d[gr[vc.pos][i].to] = nc;
          que.push({gr[vc.pos][i].to, d[gr[vc.pos][i].to]});
        }
      }
    }

    return d;
  }
};

lint f(lint x, lint y, lint d) { return h * w * d + w * y + x; }

template <class Tv, class Tl, class Tr> bool between(Tv val, Tl l, Tr r) {
  return (val >= l) && (val <= r);
}

int main() {
  cin >> h >> w >> k;
  xy p1, p2;
  cin >> p1.y >> p1.x >> p2.y >> p2.x;
  p1.x--;
  p1.y--;
  p2.x--;
  p2.y--;
  vecs c(h);
  rep(i, h) cin >> c[i];

  auto is = [&](lint x, lint y) {
    if (!between(x, 0, w - 1) || !between(y, 0, h - 1) || c[y][x] == '@')
      return false;
    return true;
  };

  graph g(4 * h * w);

  rep(i, h) rep(j, w) {
    if (is(j + 1, i)) {
      g.add_e(f(j, i, 0), f(j + 1, i, 0), 1);
    }
    if (is(j - 1, i)) {
      g.add_e(f(j, i, 1), f(j - 1, i, 1), 1);
    }
    if (is(j, i + 1)) {
      g.add_e(f(j, i, 2), f(j, i + 1, 2), 1);
    }
    if (is(j, i - 1)) {
      g.add_e(f(j, i, 3), f(j, i - 1, 3), 1);
    }

    rep(di, 4) rep(dj, 4) { g.add_e(f(j, i, di), f(j, i, dj), 0, true); }
  }

  lint ans = inf;
  // rep(i, 4)
  {
    auto d = g.dijkstra(f(p1.x, p1.y, 0));
    rep(j, 4) { chmin(ans, d[f(p2.x, p2.y, j)]); }
  }

  if (ans == inf) {
    cout << -1 << endl;
    ret;
  }

  cout << div2(ans, k) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, n) for (lint i = 0; i < n; i++)
#define repr(i, n) for (lint i = n - 1; i >= 0; i--)
#define repi(i, ini, n) for (lint i = ini; i < n; i++)
#define repir(i, ini, n) for (lint i = n - 1; i >= ini; i--)
#define repb(i, start, end) for (lint i = start; i <= end; i++)
#define repbr(i, start, end) for (lint i = end; i >= start; i--)

#define bit(n) (1LL << (n))

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rg(v, ini, end) v.begin() + ini, v.begin() + end

#define ret return 0;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

using lint = long long;
using ulint = unsigned long long;
using ld = long double;
struct xy {
  lint x, y;
  xy() : x(0), y(0) {}
  xy(lint _x, lint _y) : x(_x), y(_y) {}
  xy operator+(const xy &p) const { return xy(x + p.x, y + p.y); }
  bool operator<(xy p) const {
    if (y == p.y)
      return x < p.x;
    return y < p.y;
  }
};
struct xyd {
  ld x, y;
  xyd() : x(0), y(0) {}
  xyd(long double _x, long double _y) : x(_x), y(_y) {}
};
using vec = vector<lint>;
using vecd = vector<ld>;
using vecs = vector<string>;
using vecp = vector<xy>;
template <class T> using vect = vector<T>;
class vec2 : public vector<vector<lint>> {
public:
  vec2() {}
  vec2(lint n) : vector(n) {}
  vec2(lint h, lint w) : vector(h, vector<lint>(w)) {}
  vec2(lint h, lint w, lint v) : vector(h, vector<lint>(w, v)) {}
};

template <class T> using priq = priority_queue<T>;
template <class T> using rpriq = priority_queue<T, vector<T>, greater<T>>;
template <class Key, class Val> using hashmap = unordered_map<Key, Val>;
template <class Key> using hashset = unordered_set<Key>;

mt19937 mtrand((random_device())());

const double pi = 3.141592653589793238462;
const lint intmax = 9223372036854775807;
const lint inf = 1100100100100100100LL;

constexpr lint div2(lint p, lint q) { return (p + q - 1) / q; }

#if (__cplusplus < 201703L)

lint gcd(lint a, lint b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (!b)
      break;
    a %= b;
  }
  return a;
}

lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }

#endif

template <class T> struct nval {
  lint n; // counts
  T val;
  nval() : n(0){};
  nval(lint _n, T _val) : n(_n), val(_val){};
};

template <class It, class It2> auto spacel(It i, It2 end) {
  if (i + 1 == end) {
    return '\\n';
  } else {
    return ' ';
  }
}

ostream &setp(ostream &ost) {
  cout << setprecision(60) << fixed;
  return ost;
}

const ulint mod = 1000000007;
// const ulint mod = 998244353;
const ld eps = 0.0000001;

lint h, w, k;

struct graph {
  struct R {
    lint to, c;
    bool f;
  };

  lint v, e;

  vector<vector<R>> gr;

  graph(lint _v) : v(_v), e(0), gr(v) {}

  void add_e(lint from, lint to, lint cost, bool f = false) {
    gr[from].push_back({to, cost, f});
    e++;
  }

  auto dijkstra(lint start) {
    vector<lint> d(v, inf);

    d[start] = 0;

    struct Vc {
      lint pos, d;
      bool operator<(const Vc &p) const { return d > p.d; }
    };

    priority_queue<Vc> que;
    que.push({start, 0});

    while (!que.empty()) {
      auto vc = que.top();
      que.pop();

      rep(i, gr[vc.pos].size()) {
        lint nc = vc.d + gr[vc.pos][i].c;
        if (gr[vc.pos][i].f)
          nc = div2(nc, k) * k;
        if (nc < d[gr[vc.pos][i].to]) {
          d[gr[vc.pos][i].to] = nc;
          que.push({gr[vc.pos][i].to, d[gr[vc.pos][i].to]});
        }
      }
    }

    return d;
  }
};

lint f(lint x, lint y, lint d) { return h * w * d + w * y + x; }

template <class Tv, class Tl, class Tr> bool between(Tv val, Tl l, Tr r) {
  return (val >= l) && (val <= r);
}

int main() {
  cin >> h >> w >> k;
  xy p1, p2;
  cin >> p1.y >> p1.x >> p2.y >> p2.x;
  p1.x--;
  p1.y--;
  p2.x--;
  p2.y--;
  vecs c(h);
  rep(i, h) cin >> c[i];

  auto is = [&](lint x, lint y) {
    if (!between(x, 0, w - 1) || !between(y, 0, h - 1) || c[y][x] == '@')
      return false;
    return true;
  };

  graph g(4 * h * w);

  rep(i, h) rep(j, w) {
    if (is(j + 1, i)) {
      g.add_e(f(j, i, 0), f(j + 1, i, 0), 1);
    }
    if (is(j - 1, i)) {
      g.add_e(f(j, i, 1), f(j - 1, i, 1), 1);
    }
    if (is(j, i + 1)) {
      g.add_e(f(j, i, 2), f(j, i + 1, 2), 1);
    }
    if (is(j, i - 1)) {
      g.add_e(f(j, i, 3), f(j, i - 1, 3), 1);
    }

    rep(di, 4) rep(dj, 4) { g.add_e(f(j, i, di), f(j, i, dj), 0, true); }
  }

  lint ans = inf;
  rep(i, 4) {
    auto d = g.dijkstra(f(p1.x, p1.y, i));
    rep(j, 4) { chmin(ans, d[f(p2.x, p2.y, j)]); }
  }

  if (ans == inf) {
    cout << -1 << endl;
    ret;
  }

  cout << div2(ans, k) << endl;

  return 0;
}