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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_W 10000
#define MAX_H 10000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int field[MAX_H][MAX_W], W, H, N, Y, X;
int dy[] = {0, -1, 0, 1, -1, 1}, dx[] = {-1, 0, 1, 0, -1, 1};

int shortestPath(int sy, int sx, int ey, int ex) {
  /*  queue<pii>q;
  map<pi, int>m;
  q.push(MP(0, MP(sy, sx)));
  m[MP(sy, sx)] = 1;

  while(!q.empty()){
    pii now = q.front(); q.pop();
    //    printf("%d %d %d\\n", now.F, now.S.F, now.S.S);
    if(now.S.F == ey && now.S.S == ex){
      return now.F;
    }
    rep(d, 6){
      int ny = now.S.F + dy[d], nx = now.S.S + dx[d];
      if(ny < 0 || ny >= H || nx < 0 || nx >= W || m.count(MP(ny, nx)))
  continue; q.push(MP(now.F + 1, MP(ny, nx))); m[MP(ny, nx)] = 1;
    }
  }
  return 0;*/
  if ((sy <= ey && sx <= ex) || (sy >= ey && sx >= ex))
    return max(abs(sy - ey), abs(sx - ex));
  return abs(sy - ey) + abs(sx - ex);
}

int main() {
  scanf("%d%d%d", &W, &H, &N);
  int sy, sx, ey, ex;
  int res = 0;
  scanf("%d%d", &sx, &sy);
  sy--;
  sx--;
  rep(i, N - 1) {
    scanf("%d%d", &ex, &ey);
    ey--;
    ex--;
    res += shortestPath(sy, sx, ey, ex);
    sy = ey, sx = ex;
  }
  printf("%d\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_W 10000
#define MAX_H 10000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int field[MAX_H][MAX_W], W, H, N, Y, X;
int dy[] = {0, -1, 0, 1, -1, 1}, dx[] = {-1, 0, 1, 0, -1, 1};

int shortestPath(int sy, int sx, int ey, int ex) {
  queue<pii> q;
  map<pi, int> m;
  q.push(MP(0, MP(sy, sx)));
  m[MP(sy, sx)] = 1;

  while (!q.empty()) {
    pii now = q.front();
    q.pop();
    //    printf("%d %d %d\\n", now.F, now.S.F, now.S.S);
    if (now.S.F == ey && now.S.S == ex) {
      return now.F;
    }
    rep(d, 6) {
      int ny = now.S.F + dy[d], nx = now.S.S + dx[d];
      if (ny < 0 || ny >= H || nx < 0 || nx >= W || m.count(MP(ny, nx)))
        continue;
      q.push(MP(now.F + 1, MP(ny, nx)));
      m[MP(ny, nx)] = 1;
    }
  }
  return 0;
}

int main() {
  scanf("%d%d%d", &W, &H, &N);
  int sy, sx, ey, ex;
  int res = 0;
  scanf("%d%d", &sx, &sy);
  sy--;
  sx--;
  rep(i, N - 1) {
    scanf("%d%d", &ex, &ey);
    ey--;
    ex--;
    res += shortestPath(sy, sx, ey, ex);
    sy = ey, sx = ex;
  }
  printf("%d\\n", res);
  return 0;
}