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

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
#define fi first
#define se second

inline int dist(P a, P b) { return abs(a.fi - b.fi) + abs(a.se - b.se); }

struct Data {
  P p;
  int type;

  Data(P p, int type) : p(p), type(type) {}
  Data() {}

  bool operator<(const Data &d) const {
    if (p != d.p)
      return p < d.p;
    return type < d.type;
  }
};
int H, W;
char fld[1001][1001];
int dp[5][5][1000];
void solve() {

  P Start, Goal;
  vector<P> Monster[5];
  fill_n(**dp, 25000, INT_MAX);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c = fld[i][j];
      P p(i, j);
      if (c == 'S')
        Start = p;
      else if (c == 'G')
        Goal = p;
      else {
        for (int k = 0; k < 5; k++) {
          if (c == k + '1') {
            Monster[k].push_back(p);
            break;
          }
        }
      }
    }
  }

  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < Monster[i].size(); j++) {
      P p = Monster[i][j];
      dp[0][i][j] = dist(p, Start);
    }
  }

  for (int k = 0; k < 3; k++) {
    for (int i = 0; i < 5; i++) {
      int to = (i + 1) % 5;
      for (int j = 0; j < Monster[i].size(); j++) {
        if (dp[k][i][j] == INT_MAX)
          continue;
        P prev = Monster[i][j];
        for (int l = 0; l < Monster[to].size(); l++) {
          P next = Monster[to][l];
          dp[k + 1][to][l] =
              min(dp[k + 1][to][l], dp[k][i][j] + dist(prev, next));
        }
      }
    }
  }
  P ans(INT_MAX, 0);
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < Monster[i].size(); j++) {
      if (dp[3][i][j] == INT_MAX)
        continue;
      ans = min(ans, P(dist(Monster[i][j], Goal) + dp[3][i][j], (i + 1) % 5));
    }
  }

  if (ans.first == INT_MAX)
    cout << "NA" << endl;
  else
    cout << ans.second + 1 << " " << ans.first << endl;
}

int main() {
  while (scanf("%d%d", &W, &H), W || H) {
    for (int i = 0; i < H; i++)
      scanf("%s", fld[i]);
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
#define fi first
#define se second

inline int dist(P a, P b) { return abs(a.fi - b.fi) + abs(a.se - b.se); }

struct Data {
  P p;
  int type;

  Data(P p, int type) : p(p), type(type) {}
  Data() {}

  bool operator<(const Data &d) const {
    if (p != d.p)
      return p < d.p;
    return type < d.type;
  }
};
int H, W;
char fld[1001][1001];
void solve() {

  P Start, Goal;
  vector<P> Monster[5];

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c = fld[i][j];
      P p(i, j);
      if (c == 'S')
        Start = p;
      else if (c == 'G')
        Goal = p;
      else {
        for (int k = 0; k < 5; k++) {
          if (c == k + '1') {
            Monster[k].push_back(p);
            break;
          }
        }
      }
    }
  }

  map<Data, int> prev;
  for (int i = 0; i < 5; i++) {
    prev[Data(Start, i)] = 0;
  }

  for (int i = 0; i < 4; i++) {
    map<Data, int> next;
    for (map<Data, int>::iterator it = prev.begin(); it != prev.end(); it++) {
      int to = (it->first.type + 1) % 5;
      for (int j = 0; j < Monster[to].size(); j++) {
        P &p = Monster[to][j];
        Data d(p, to);
        if (next.find(d) != next.end())
          next[d] = min(next[d], it->second + dist(it->first.p, p));
        else
          next[d] = it->second + dist(it->first.p, p);
      }
    }
    prev = next;
  }
  P ans(INT_MAX, 0);
  for (map<Data, int>::iterator it = prev.begin(); it != prev.end(); it++) {
    ans = min(ans, P(dist(it->first.p, Goal) + it->second, it->first.type));
  }

  if (ans.first == INT_MAX)
    cout << "NA" << endl;
  else
    cout << (ans.second + 2) % 5 << " " << ans.first << endl;
}

int main() {
  cin.tie();
  ios_base::sync_with_stdio(false);

  while (scanf("%d%d", &W, &H), W || H) {
    for (int i = 0; i < H; i++)
      scanf("%s", fld[i]);
    solve();
  }
}