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

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

#define INF (1 << 25)

int sx, sy, gx, gy, W, H;
int px[1000][5], py[1000][5], pn[5];
int dp[1000][1000][5];

void init() {
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      for (int k = 0; k < 5; k++)
        dp[j][i][k] = INF;
}

int dist(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); }

void calc(int n) {
  int b = (n + 1) % 5;
  for (int i = 0; i < pn[b]; i++) {
    int x = px[i][b], y = py[i][b];
    dp[x][y][b] = min(dp[x][y][b], dist(x, y, sx, sy));
  }

  for (int i = 2; i < 5; i++) {
    int l = (i + n) % 5, p = (i + n - 1) % 5;
    for (int j = 0; j < pn[p]; j++) {
      int s = px[j][p], t = py[j][p];
      if (dp[s][t][p] == INF)
        continue;

      for (int m = 0; m < pn[l]; m++) {
        int x = px[m][l], y = py[m][l];
        dp[x][y][l] = min(dp[x][y][l], dp[s][t][p] + dist(x, y, s, t));

        dp[gx][gy][l] = min(dp[gx][gy][l], dp[x][y][l] + dist(x, y, gx, gy));
      }
    }
  }
}

int main() {
  while (cin >> W >> H, (W || H)) {
    memset(pn, 0, sizeof(pn));

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        char t;
        cin >> t;

        if (t == '.')
          continue;
        if (isdigit(t)) {
          int n = t - '1';
          px[pn[n]][n] = j;
          py[pn[n]][n] = i;

          pn[n]++;
        }

        if (t == 'S') {
          sx = j;
          sy = i;
        }

        if (t == 'G') {
          gx = j;
          gy = i;
        }
      }

    int v, m = INF;

    for (int i = 0; i < 5; i++) {
      init();
      calc(i);

      if (m > dp[gx][gy][(i + 4) % 5]) {
        v = i + 1;
        m = dp[gx][gy][(i + 4) % 5];
      }
    }

    if (m == INF)
      cout << "NA" << endl;
    else
      cout << v << " " << m << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

#define INF (1 << 25)

int sx, sy, gx, gy, W, H;
int px[1000][5], py[1000][5], pn[5];
int dp[1000][1000][5];

void init() {
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      for (int k = 0; k < 5; k++)
        dp[i][j][k] = INF;
}

int dist(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); }

void calc(int n) {
  for (int i = 1; i < 5; i++)
    for (int j = 0; j < H; j++)
      for (int k = 0; k < W; k++) {
        int l = (i + n) % 5, p = (i - 1 + n) % 5;
        if (dp[k][j][i] == INF)
          continue;

        for (int m = 0; m < pn[l]; m++) {
          int x = px[m][l], y = py[m][l];
          dp[x][y][l] = min(dp[x][y][l], dp[k][j][p] + dist(x, y, k, j));

          dp[gx][gy][l] = min(dp[gx][gy][l], dp[x][y][l] + dist(x, y, gx, gy));
        }
      }
}

int main() {

  while (cin >> W >> H, (W || H)) {
    memset(pn, 0, sizeof(pn));

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        char t;
        cin >> t;

        if (t == '.')
          continue;
        if (isdigit(t)) {
          int n = t - '1';
          px[pn[n]][n] = j;
          py[pn[n]][n] = i;

          pn[n]++;
        }

        if (t == 'S') {
          sx = j;
          sy = i;
        }

        if (t == 'G') {
          gx = j;
          gy = i;
        }
      }

    int v, m = INF;

    init();

    for (int i = 0; i < 5; i++) {
      init();
      dp[sx][sy][i] = 0;
      calc(i);

      if (m > dp[gx][gy][(i + 4) % 5]) {
        v = i + 1;
        m = dp[gx][gy][(i + 4) % 5];
      }
    }

    if (m == INF)
      cout << "NA" << endl;
    else
      cout << v << " " << m << endl;
  }
}