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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
// typedef pair<int, P> PP;

const int INF = 1 << 25;
int dp[5][1005];

int main() {
  //	cut here before submit
  //	freopen ("testcase.Pachimon_Creature", "r", stdin );
  int w, h;
  while (cin >> w >> h && w && h) {
    vector<string> m(h);
    rep(i, h) m[i].clear();

    P s, g;
    vector<P> p[5];
    rep(i, 5) p[i].clear();

    rep(i, h) { cin >> m[i]; } // end rep
    rep(i, h) {
      rep(j, w) {
        if (m[i][j] == 'S')
          s = P(i, j);
        else if (m[i][j] == 'G')
          g = P(i, j);
        else if (isdigit(m[i][j]))
          p[m[i][j] - '0' - 1].push_back(P(i, j));
      } // end rep
    }   // end rep

    int first_cur = -1;
    int res = INF;
    for (int cur = 0; cur < 5; cur++) {
      rep(i, 5) rep(j, 1005) dp[i][j] = INF;
      int nxt = (cur + 1) % 5;
      // S ツつゥツづァ ツ堕ョツ青ォ:nxt ツづ鳴づ個経ツ路
      for (int j = 0; j < p[nxt].size(); j++) {
        dp[nxt][j] =
            abs(p[nxt][j].first - s.first) + abs(p[nxt][j].second - s.second);
      } // end for
      for (int i = nxt, cnt = 0; cnt < 5; cnt++, i = (i + 1) % 5) {
        for (int j = 0; j < p[(i + 1) % 5].size(); j++) {
          for (int k = 0; k < p[i].size(); k++) {
            int l = abs(p[(i + 1) % 5][j].first - p[i][k].first) +
                    abs(p[(i + 1) % 5][j].second - p[i][k].second);
            dp[(i + 1) % 5][j] = min(dp[(i + 1) % 5][j], dp[i][k] + l);
          } // end for
        }   // end for
      }     // end for
      // ツ堕ョツ青ォ: (cur-1+5) % 5 ツつゥツづァ G ツづ鳴づ個経ツ路
      for (int j = 0; j < p[(cur - 1 + 5) % 5].size(); j++) {
        int l = dp[(cur - 1 + 5) % 5][j] +
                abs(g.first - p[(cur - 1 + 5) % 5][j].first) +
                abs(g.second - p[(cur - 1 + 5) % 5][j].second);
        if (res > l) {
          res = l;
          first_cur = cur;
        } // end if
      }   // end for
    }     // end for

    if (res == INF)
      cout << -1 << endl;
    else
      cout << first_cur + 1 << ' ' << res << endl;

  } // end loop

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

const int INF = 1 << 30;
vector<PP> dp[5];

int main() {
  //	cut here before submit
  //	freopen ("testcase.Pachimon_Creature", "r", stdin );
  int w, h;
  while (cin >> w >> h && w && h) {
    vector<string> m(h);
    rep(i, h) m[i].clear();

    P s, g;
    vector<P> p[5];
    rep(i, 5) p[i].clear();

    rep(i, h) { cin >> m[i]; } // end rep
    rep(i, h) {
      rep(j, w) {
        if (m[i][j] == 'S')
          s = P(i, j);
        else if (m[i][j] == 'G')
          g = P(i, j);
        else if (isdigit(m[i][j]))
          p[m[i][j] - '0' - 1].push_back(P(i, j));
      } // end rep
    }   // end rep

    int first_give = -1;
    int res = INF;

    for (int give = 0; give < 5; give++) {
      rep(i, 5) dp[i].clear();
      int f = (give + 1) % 5;
      for (int j = 0; j < p[f].size(); j++) {
        PP pp =
            PP(abs(p[f][j].first - s.first) + abs(p[f][j].second - s.second),
               p[f][j]);
        dp[f].push_back(pp);
      } // end for
      for (int i = f, cnt = 0; cnt < 5; cnt++, i = (i + 1) % 5) {
        for (int j = 0; j < p[(i + 1) % 5].size(); j++) {
          int mind = INF;
          for (int k = 0; k < dp[i].size(); k++) {
            int l = abs(p[(i + 1) % 5][j].first - dp[i][k].second.first) +
                    abs(p[(i + 1) % 5][j].second - dp[i][k].second.second);
            int curr = dp[i][k].first + l;
            if (curr <= mind) {
              mind = curr;
              PP pp = PP(curr, p[(i + 1) % 5][j]);
              dp[(i + 1) % 5].push_back(pp);
            } // end if
          }   // end for
        }     // end for
      }       // end for
      for (int j = 0; j < dp[(give - 1 + 5) % 5].size(); j++) {
        int l = dp[(give - 1 + 5) % 5][j].first +
                abs(g.first - dp[(give - 1 + 5) % 5][j].second.first) +
                abs(g.second - dp[(give - 1 + 5) % 5][j].second.second);
        if (res > l) {
          res = l;
          first_give = give;
        } // end if
      }   // end for
    }     // end for
    if (res == INF)
      cout << -1 << endl;
    else
      cout << first_give + 1 << ' ' << res << endl;

  } // end loop

  return 0;
}