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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define INF (1 << 30)
#define fr first
#define sc second
typedef pair<int, int> Pi;
typedef pair<Pi, Pi> Pii;
const int dy[] = {0, 1, -1, 0}, dx[] = {1, 0, 0, -1};
int h, w, Sx, Sy, Gx, Gy;
vector<Pi> hoge[5];
int dp[5][1000][1000], num;
char c;
int dis(Pi x1, Pi x2) { return abs(x1.fr - x2.fr) + abs(x1.sc - x2.sc); }
int rec(int now, int y, int x) {
  if (dp[now][y][x] != -1)
    return dp[now][y][x];
  int ret = INF, next = (now + 1) % 5;
  if (now == num)
    ret = dis(Pi(y, x), Pi(Gy, Gx));
  else
    for (int i = 0; i < hoge[next].size(); i++) {
      ret = min(ret, rec(next, hoge[next][i].fr, hoge[next][i].sc) +
                         dis(Pi(y, x), hoge[next][i]));
    }
  return dp[now][y][x] = ret;
}
int main() {
  while (cin >> w >> h, w) {
    for (int i = 0; i < 5; i++)
      hoge[i].clear();
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> c;
        if (c == 'S')
          Sy = i, Sx = j;
        else if (c == 'G')
          Gy = i, Gx = j;
        else if (c != '.')
          hoge[c - '1'].push_back(Pi(i, j));
      }
    }
    Pi ret = Pi(INF, INF);
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++)
        for (int k = 0; k < h; k++)
          for (int l = 0; l < w; l++)
            dp[j][k][l] = -1;
      num = (i + 4) % 5;
      ret = min(ret, Pi(rec(i, Sy, Sx), i + 1));
    }
    if (ret.fr == INF)
      cout << "NA" << endl;
    else
      cout << ret.sc << " " << ret.fr << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF (1 << 30)
#define fr first
#define sc second
typedef pair<int, int> Pi;
typedef pair<Pi, Pi> Pii;
const int dy[] = {0, 1, -1, 0}, dx[] = {1, 0, 0, -1};
int h, w, Sx, Sy;
char mas[1000][1000];
bool used[5][1000][1000];
int Dijkstra(int now) {
  int gl = (now + 4) % 5;
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < h; j++) {
      for (int k = 0; k < w; k++) {
        used[i][j][k] = false;
      }
    }
  }
  priority_queue<Pii, vector<Pii>, greater<Pii>> que;
#define NEXT (p.fr.sc + 1) % 5
  que.push(Pii(Pi(0, now), Pi(Sy, Sx)));
  used[now][Sy][Sx] = true;
  while (!que.empty()) {
    Pii p = que.top();
    que.pop();
    if (p.fr.sc == gl && mas[p.sc.fr][p.sc.sc] == 'G')
      return p.fr.fr;
    for (int i = 0; i < 4; i++) {
      int ny = p.sc.fr + dy[i], nx = p.sc.sc + dx[i], dan = p.fr.sc;
      if (ny < 0 || nx < 0 || ny >= h || nx >= w)
        continue;
      if (dan != gl && mas[ny][nx] - '0' == NEXT)
        dan = (dan + 1) % 5;
      if (used[dan][ny][nx]++)
        continue;
      que.push(Pii(Pi(p.fr.fr + 1, dan), Pi(ny, nx)));
    }
  }
  return INF;
}
int main() {
  while (scanf("%d %d", &w, &h), w) {
    for (int i = 0; i < h; i++) {
      scanf("%s", mas[i]);
      for (int j = 0; j < w; j++) {
        if (mas[i][j] == 'S')
          Sy = i, Sx = j;
        else if (isdigit(mas[i][j]))
          --mas[i][j];
      }
    }
    Pi ret = Pi(INF, INF);
    for (int i = 0; i < 5; i++)
      ret = min(ret, Pi(Dijkstra(i), i + 1));
    if (ret.fr == INF)
      printf("NA\\n");
    else
      printf("%d %d\\n", ret.sc, ret.fr);
  }
}