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

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

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

typedef pair<int, pii> State;

int H, W, N;
char m[1010][1010];

vi bfs(int x, int y) {
  vi res(N + 1, 0);

  queue<pair<int, pii>> q;
  q.push(mp(0, mp(x, y)));
  map<pii, bool> visited;

  while (!q.empty()) {
    pair<int, pii> p = q.front();
    q.pop();
    if (visited[p.snd])
      continue;
    visited[p.snd] = true;

    if (isdigit(m[p.snd.snd][p.snd.fst])) {
      res[m[p.snd.snd][p.snd.fst] - '0'] = p.fst;
    }
    if (m[p.snd.snd][p.snd.fst] == 'S') {
      res[0] = p.fst;
    }

    static const int dx[] = {1, 0, -1, 0};
    static const int dy[] = {0, 1, 0, -1};
    rep(d, 4) {
      int tx = p.snd.fst + dx[d];
      int ty = p.snd.snd + dy[d];
      if (tx < 0 || tx >= W || ty < 0 || ty >= H)
        continue;
      if (m[ty][tx] == 'X')
        continue;
      q.push(mp(p.fst + 1, mp(tx, ty)));
    }
  }

  return res;
}

int main() {
  cin >> H >> W >> N;
  pii start;
  rep(i, H) rep(j, W) {
    cin >> m[i][j];
    if (m[i][j] == 'S') {
      start.fst = j;
      start.snd = i;
    }
  }

  vvi mt(N + 1);
  rep(i, H) rep(j, W) {
    if (isdigit(m[i][j]))
      mt[m[i][j] - '0'] = bfs(j, i);
    else if (m[i][j] == 'S')
      mt[0] = bfs(j, i);
  }

  priority_queue<State, vector<State>, greater<State>> q;
  q.push(mp(0, mp(0, 0)));
  map<pii, bool> visited;

  while (!q.empty()) {
    State st = q.top();
    q.pop();
    if (visited[st.snd])
      continue;
    visited[st.snd] = true;

    int pos = st.snd.snd;
    int ate = st.snd.fst;
    if (__builtin_popcount(ate) == N) {
      cout << st.fst << endl;
      break;
    }
    int hp = 1 + __builtin_popcount(ate);
    rep(i, N) if ((ate & 1 << i + 1) == 0) if (hp >= i + 1) {
      q.push(mp(st.fst + mt[pos][i + 1], mp(ate | 1 << i + 1, i + 1)));
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

typedef pair<int, pair<int, pii>> State;

int H, W, N;
char m[1010][1010];

int main() {
  cin >> H >> W >> N;
  pii start;
  rep(i, H) rep(j, W) {
    cin >> m[i][j];
    if (m[i][j] == 'S') {
      start.fst = j;
      start.snd = i;
    }
  }

  priority_queue<State, vector<State>, greater<State>> q;
  q.push(mp(0, mp(0, start)));
  map<pair<int, pii>, bool> visited;

  while (!q.empty()) {
    State st = q.top();
    q.pop();
    if (visited[st.snd])
      continue;
    visited[st.snd] = true;

    int x = st.snd.snd.fst, y = st.snd.snd.snd;
    int ate = st.snd.fst;
    if (__builtin_popcount(ate) == N) {
      cout << st.fst << endl;
      break;
    }
    static const int dx[] = {1, 0, -1, 0};
    static const int dy[] = {0, 1, 0, -1};
    rep(d, 4) {
      int tx = x + dx[d];
      int ty = y + dy[d];
      if (tx < 0 || tx >= W || ty < 0 || ty >= H)
        continue;
      if (m[ty][tx] == 'X')
        continue;
      if (isdigit(m[ty][tx])) {
        if (ate & 1 << m[ty][tx] - '0')
          continue;
        int hp = 1 + __builtin_popcount(ate);
        if (hp >= m[ty][tx] - '0') {
          q.push(mp(st.fst + 1, mp(ate | 1 << m[ty][tx] - '0', mp(tx, ty))));
          continue;
        }
      }
      q.push(mp(st.fst + 1, mp(ate, mp(tx, ty))));
    }
  }

  return 0;
}