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

#include <algorithm>
#include <complex>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1 << 30
using namespace std;
typedef pair<int, int> mypair;
typedef pair<mypair, mypair> pi;
int dx[7] = {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}};
int main() {
  int sx, sy, gx, gy, n, lx, ly;
  int dp[201][201][6];
  for (int i = 0; i < 201; i++)
    for (int j = 0; j < 201; j++)
      for (int k = 0; k < 6; k++)
        dp[i][j][k] = INF;
  // fill(dp[0][0],dp[201][0],INF);
  bool ob[201][201] = {};

  cin >> sx >> sy >> gx >> gy >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    ob[100 + x][100 + y] = true;
  }

  cin >> lx >> ly;

  priority_queue<pi> q;
  q.push(pi(mypair(0, 0), mypair(sx, sy)));
  //	q.push((S){sx,sy,0,0});
  while (!q.empty()) {
    pi temp = q.top();
    q.pop();
    int ti = temp.first.first, p = temp.first.second, x = temp.second.first,
        y = temp.second.second;
    // int x=temp.x ,y=temp.y ,ti temp.t ,p=temp.p;
    if (dp[x + 100][y + 100][-ti] <= -p)
      continue;
    dp[x + 100][y + 100][-ti] = -p;

    for (int i = 0; i < 7; i++) {
      int nx = x + dx[i];
      int ny = y + dy[abs(x) % 2][i];
      if (abs(ny) <= ly && abs(nx) <= lx && !ob[nx + 100][ny + 100]) {

        q.push(pi(mypair((ti - 1) % 6, p - (abs(x * y * ti) % 6 != i)),
                  mypair(nx, ny)));
        //		q.push((S){nx,ny,(ti+1)%6,p+(abs(x*y*ti)%6!=i)});
      }
    }
  }
  int ans = INF;

  for (int i = 0; i < 6; i++) {
    ans = min(ans, dp[gx + 100][gy + 100][i]);
    //	cout<<dp[gx+100][gy+100][i]<<endl;
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1 << 30
using namespace std;
typedef pair<int, int> mypair;
typedef pair<mypair, mypair> pi;
int dx[7] = {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}};

int main() {
  int sx, sy, gx, gy, n, lx, ly;
  int dp[201][201][6];
  for (int i = 0; i < 201; i++)
    for (int j = 0; j < 201; j++)
      for (int k = 0; k < 6; k++)
        dp[i][j][k] = INF;

  bool ob[201][201] = {};

  cin >> sx >> sy >> gx >> gy >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    ob[100 + x][100 + y] = true;
  }

  cin >> lx >> ly;

  priority_queue<pi> q;
  q.push(pi(mypair(0, 0), mypair(sx, sy)));
  while (!q.empty()) {
    pi temp = q.top();
    q.pop();
    int x = temp.second.first, y = temp.second.second, ti = temp.first.first,
        p = temp.first.second;
    if (dp[x + 100][y + 100][ti] <= p)
      continue;
    dp[x + 100][y + 100][ti] = p;

    for (int i = 0; i < 7; i++) {
      int nx = x + dx[i];
      int ny = y + dy[abs(x) % 2][i];
      if (-ly <= y && y <= ly && -lx <= x && x <= lx && !ob[x + 100][y + 100]) {

        q.push(pi(mypair((ti + 1) % 6, p + (abs(x * y * ti) % 6 != i)),
                  mypair(nx, ny)));
      }
    }
  }
  int ans = INF;

  for (int i = 0; i < 6; i++) {
    ans = min(ans, dp[gx + 100][gy + 100][i]);
    // cout<<dp[gx+100][gy+100][i]<<endl;
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}