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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
const int INF = 1e9;

int dx[2][7] = {
    {+0, +1, +1, +0, -1, -1, 0},
    {+0, +1, +1, +0, -1, -1, 0},
};
int dy[2][7] = {
    {+1, +0, -1, -1, -1, +0, 0},
    {+1, +1, +0, -1, +0, +1, 0},
};

signed main() {
  int w, h, sx, sy, gx, gy, n;
  cin >> sx >> sy >> gx >> gy;
  cin >> n;
  vector<int> vx(n), vy(n);
  rep(i, n) { cin >> vx[i] >> vy[i]; }
  cin >> w >> h;
  map<int, map<int, int>> mp;
  map<int, int> s;
  for (int y = -h; y <= h; y++) {
    s[y] = INF;
  }
  {
    typedef pair<int, map<int, int>> pm;
    queue<pm> q;
    q.push(make_pair(0, s));
    while (q.size()) {
      auto x = q.front().first;
      auto t = q.front().second;
      q.pop();
      if (mp.count(x))
        continue;
      mp[x] = t;
      if (abs(x) + 1 > w)
        continue;
      q.push(make_pair(x + 1, t));
      q.push(make_pair(x - 1, t));
    }
  }
  auto v = mp;
  rep(i, n) { v[vx[i]][vy[i]] = 1; }
  auto check = [&](int x, int y) {
    if (abs(x) <= w && abs(y) <= h) {
      if (v[x][y] == 1)
        return false;
      return true;
    }
    return false;
  };
  vector<map<int, map<int, int>>> me(6, mp);
  typedef tuple<int, int, int, int> T;
  priority_queue<T, vector<T>, greater<T>> q;
  q.push(T(0, sx, sy, 0));
  me[0][sx][sy] = 0;
  while (q.size()) {
    int x = get<1>(q.top()), y = get<2>(q.top()), t = get<3>(q.top());
    q.pop();
    int d = abs(x % 2);
    int nt = (t + 1) % 6;
    {
      int j = abs(x * y * t) % 6;
      int tx = x + dx[d][j], ty = y + dy[d][j];
      if (check(tx, ty) && me[nt][tx][ty] > me[t][x][y]) {
        me[nt][tx][ty] = me[t][x][y];
        q.push(T(me[nt][tx][ty], tx, ty, nt));
      }
    }
    rep(j, 7) {
      int tx = x + dx[d][j], ty = y + dy[d][j];
      if (check(tx, ty) && me[nt][tx][ty] > me[t][x][y] + 1) {
        me[nt][tx][ty] = me[t][x][y] + 1;
        q.push(T(me[nt][tx][ty], tx, ty, nt));
      }
    }
  }
  int ans = INF;
  rep(t, 6) { ans = min(ans, me[t][gx][gy]); }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
const int INF = 1e9;

int dx[2][6] = {
    {+0, +1, +1, +0, -1, -1},
    {+0, +1, +1, +0, -1, -1},
};
int dy[2][6] = {
    {+1, +0, -1, -1, -1, +0},
    {+1, +1, +0, -1, +0, +1},
};

signed main() {
  int w, h, sx, sy, gx, gy, n;
  cin >> sx >> sy >> gx >> gy;
  cin >> n;
  vector<int> vx(n), vy(n);
  rep(i, n) { cin >> vx[i] >> vy[i]; }
  cin >> w >> h;
  map<int, map<int, int>> mp;
  map<int, int> s;
  for (int y = -h; y <= h; y++) {
    s[y] = INF;
  }
  {
    typedef pair<int, map<int, int>> pm;
    queue<pm> q;
    q.push(make_pair(0, s));
    while (q.size()) {
      auto x = q.front().first;
      auto t = q.front().second;
      q.pop();
      if (mp.count(x))
        continue;
      mp[x] = t;
      if (abs(x) + 1 > w)
        continue;
      q.push(make_pair(x + 1, t));
      q.push(make_pair(x - 1, t));
    }
  }
  auto v = mp;
  rep(i, n) { v[vx[i]][vy[i]] = 1; }
  auto check = [&](int x, int y) {
    if (abs(x) <= w && abs(y) <= h) {
      if (v[x][y] == 1)
        return false;
      return true;
    }
    return false;
  };
  vector<map<int, map<int, int>>> me(6, mp);
  typedef tuple<int, int, int> T;
  queue<T> q;
  q.push(T(sx, sy, 0));
  me[0][sx][sy] = 0;
  while (q.size()) {
    int x = get<0>(q.front()), y = get<1>(q.front()), t = get<2>(q.front());
    q.pop();
    int d = abs(x % 2);
    int nt = (t + 1) % 6;
    {
      int j = abs(x * y * t) % 6;
      int tx = x + dx[d][j], ty = y + dy[d][j];
      if (check(tx, ty) && me[nt][tx][ty] > me[t][x][y]) {
        me[nt][tx][ty] = me[t][x][y];
        q.push(T(tx, ty, nt));
      }
    }
    {
      if (me[nt][x][y] > me[t][x][y] + 1) {
        me[nt][x][y] = me[t][x][y] + 1;
        q.push(T(x, y, nt));
      }
    }
    rep(j, 6) {
      int tx = x + dx[d][j], ty = y + dy[d][j];
      if (check(tx, ty) && me[nt][tx][ty] > me[t][x][y] + 1) {
        me[nt][tx][ty] = me[t][x][y] + 1;
        q.push(T(tx, ty, nt));
      }
    }
  }
  int ans = INF;
  rep(t, 6) { ans = min(ans, me[t][gx][gy]); }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}