Problem p01539 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(i, v.size()) cout << " " << v[i];                                        \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(i, s) cout << " " << i;                                                 \
  cout << endl

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 102;
const int dx[] = {0, 1, 1, 0, -1, -1, 0};
const int dy1[] = {1, 0, -1, -1, -1, 0, 0};
const int dy2[] = {1, 1, 0, -1, 0, 1, 0};
const int dt[] = {1, 1, 1, 1, 1, 1, 1};

struct edge {
  int to;
  int cost;
};

struct pos {
  int x, y, t;
  bool operator<(const pos &another) const {
    if (x != another.x) {
      return x < another.x;
    } else if (y != another.y) {
      return y < another.y;
    } else {
      return t < another.t;
    }
  }
};

int d[6 * MAX_N * MAX_N];
vector<edge> G[6 * MAX_N * MAX_N];
map<P, int> mp;
map<pos, int> zip;
pos trans[6 * MAX_N * MAX_N];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    vector<edge>::iterator it = G[v].begin();
    rep(i, G[v].size()) {
      if (d[G[v][i].to] > d[v] + G[v][i].cost) {
        d[G[v][i].to] = d[v] + G[v][i].cost;
        que.push(P(d[G[v][i].to], G[v][i].to));
      }
    }
  }
}

int main() {
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  int n;
  cin >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    mp[P(x, y)]++;
  }
  int lx, ly;
  cin >> lx >> ly;
  int cnt = 0;
  for (int i = -lx; i <= lx; i++) {
    for (int j = -ly; j <= ly; j++) {
      if (mp[P(i, j)] == 1) {
        continue;
      }
      rep(k, 6) {
        trans[cnt] = (pos){i, j, k};
        zip[(pos){i, j, k}] = cnt++;
      }
    }
  }
  rep(i, cnt) {
    int x = trans[i].x;
    int y = trans[i].y;
    int t = trans[i].t;
    int ord = abs(x * y * t) % 6;
    rep(j, 7) {
      int nx, ny, nt;
      if (x % 2 == 0) {
        nx = x + dx[j];
        ny = y + dy1[j];
        nt = (t + dt[j]) % 6;
      } else {
        nx = x + dx[j];
        ny = y + dy2[j];
        nt = (t + dt[j]) % 6;
      }
      if (mp[P(nx, ny)] == 1) {
        continue;
      }
      if (-lx <= nx && nx <= lx && -ly <= ny && ny <= ly) {
        G[i].push_back((edge){zip[(pos){nx, ny, nt}], (j == ord ? 0 : 1)});
      }
    }
  }
  rep(i, cnt) { d[i] = INF; }
  dijkstra(zip[(pos){sx, sy, 0}]);
  int ans = INF;
  rep(i, 6) { ans = min(ans, d[zip[(pos){gx, gy, i}]]); }
  if (ans == INF) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(i, v.size()) cout << " " << v[i];                                        \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(i, s) cout << " " << i;                                                 \
  cout << endl

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 200;
const int dx[] = {0, 1, 1, 0, -1, -1, 0};
const int dy1[] = {1, 0, -1, -1, -1, 0, 0};
const int dy2[] = {1, 1, 0, -1, 0, 1, 0};
const int dt[] = {1, 1, 1, 1, 1, 1, 1};

struct edge {
  int to;
  int cost;
};

struct pos {
  int x, y, t;
  bool operator<(const pos &another) const {
    if (x != another.x) {
      return x < another.x;
    } else if (y != another.y) {
      return y < another.y;
    } else {
      return t < another.t;
    }
  }
};

int d[6 * MAX_N * MAX_N];
vector<edge> G[6 * MAX_N * MAX_N];
map<P, int> mp;
map<pos, int> zip;
pos trans[6 * MAX_N * MAX_N];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    vector<edge>::iterator it = G[v].begin();
    rep(i, G[v].size()) {
      if (d[G[v][i].to] > d[v] + G[v][i].cost) {
        d[G[v][i].to] = d[v] + G[v][i].cost;
        que.push(P(d[G[v][i].to], G[v][i].to));
      }
    }
  }
}

int main() {
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  int n;
  cin >> n;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    mp[P(x, y)]++;
  }
  int lx, ly;
  cin >> lx >> ly;
  int cnt = 0;
  for (int i = -lx; i <= lx; i++) {
    for (int j = -ly; j <= ly; j++) {
      if (mp[P(i, j)] == 1) {
        continue;
      }
      rep(k, 6) {
        trans[cnt] = (pos){i, j, k};
        zip[(pos){i, j, k}] = cnt++;
      }
    }
  }
  rep(i, cnt) {
    int x = trans[i].x;
    int y = trans[i].y;
    int t = trans[i].t;
    int ord = abs(x * y * t) % 6;
    rep(j, 7) {
      int nx, ny, nt;
      if (x % 2 == 0) {
        nx = x + dx[j];
        ny = y + dy1[j];
        nt = (t + dt[j]) % 6;
      } else {
        nx = x + dx[j];
        ny = y + dy2[j];
        nt = (t + dt[j]) % 6;
      }
      if (mp[P(nx, ny)] == 1) {
        continue;
      }
      if (-lx <= nx && nx <= lx && -ly <= ny && ny <= ly) {
        G[i].push_back((edge){zip[(pos){nx, ny, nt}], (j == ord ? 0 : 1)});
      }
    }
  }
  rep(i, cnt) { d[i] = INF; }
  dijkstra(zip[(pos){sx, sy, 0}]);
  int ans = INF;
  rep(i, 6) { ans = min(ans, d[zip[(pos){gx, gy, i}]]); }
  if (ans == INF) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }
}