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

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
using namespace std;

struct S {
  int cost, x, y, o, xh;
  S(int c, int in_x, int in_y, int in_o, int in_xh)
      : cost(c), x(in_x), y(in_y), o(in_o), xh(in_xh) {}
  bool operator<(const S &s) const {
    if (cost != s.cost)
      return cost < s.cost;
    else
      return o < s.o;
  }
};

const int INF = -1000000000, INF2 = 10000000;
int w, h, f, m, o, ans;
int stratum[110][110];
int c[11][11][51];
priority_queue<S> pq;

void dijkstra() {
  int move[3] = {1, -1, 1}; //?´y,?´x,?´x
  while (!pq.empty()) {
    S a = pq.top();
    pq.pop();
    if (a.o == 0)
      continue;
    if (a.y == h) {
      if (-a.cost <= f)
        ans = -a.cost;
      break;
    }
    if (a.o == 1)
      continue;
    for (int i = 0; i < 3; i++) {
      int x = a.x;
      int y = a.y;
      if (i)
        x += move[i];
      else
        y += move[i];
      int xx = 1 << (w - x);
      xx = xx & a.xh;
      if (!xx || !i) {
        if (x <= w && 0 < x && y <= h) {
          if (stratum[y][x] < 0) {
            if (a.cost + stratum[y][x] >= -f &&
                a.cost + stratum[y][x] > c[y][x][a.o - 1]) {
              c[y][x][a.o - 1] = a.cost + stratum[y][x];
              if (i)
                pq.push({c[y][x][a.o - 1], x, y, a.o - 1, a.xh | 1 << (w - x)});
              else
                pq.push({c[y][x][a.o - 1], x, y, a.o - 1, 1 << (w - x)});
            }
          } else if (stratum[y][x] > 0) {
            int hokyuuu;
            hokyuuu = stratum[y][x] + a.o - 1;
            if (hokyuuu + a.o - 1 >= m)
              hokyuuu = m;
            c[y][x][hokyuuu] = a.cost;
            if (i)
              pq.push({c[y][x][hokyuuu], x, y, hokyuuu, a.xh | 1 << (w - x)});
            else
              pq.push({c[y][x][hokyuuu], x, y, hokyuuu, 1 << (w - x)});
          } else {
            pq.push({0, x, y, o, a.xh | 1 << (w - x)});
          }
        }
      } else {
        if (a.cost > c[y][x][a.o - 1] && a.o - 1) {
          c[y][x][a.o - 1] = a.cost;
          pq.push({c[y][x][a.o - 1], x, y, a.o - 1, a.xh});
        }
      }
    }
  }
}

int main() {
  while (cin >> w >> h && w && h) {
    cin >> f >> m >> o;
    memset(stratum, sizeof(stratum), 0);
    ans = INF2;
    for (int i = 1; i <= 10; i++)
      for (int j = 1; j <= 10; j++)
        fill(c[i][j], c[i][j] + 51, INF);
    while (!pq.empty())
      pq.pop();
    for (int i = 1; i <= h; i++)
      for (int j = 1; j <= w; j++)
        cin >> stratum[i][j];
    pq.push({0, 1, 0, o, 1 << (w - 1)});
    dijkstra();
    if (ans != INF2)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;

struct S {
  int cost, x, y, o, xh;
  S(int c, int in_x, int in_y, int in_o, int in_xh)
      : cost(c), x(in_x), y(in_y), o(in_o), xh(in_xh) {}
  bool operator<(const S &s) const {
    if (cost != s.cost)
      return cost < s.cost;
    else
      return o < s.o;
  }
};

const int INF = -1000000000, INF2 = 10000000;
int w, h, f, m, o, ans;
int stratum[11][11];
int c[11][11][51];
priority_queue<S> pq;

void dijkstra() {
  int move[3] = {1, -1, 1}; //?´y,?´x,?´x
  while (!pq.empty()) {
    S a = pq.top();
    pq.pop();
    if (a.o == 0)
      continue;
    /*if (a.y == h){
            if (-a.cost<=f)
            ans = -a.cost;
            break;
    }*/
    if (a.o == 1)
      continue;
    for (int i = 0; i < 3; i++) {
      int x = a.x;
      int y = a.y;
      if (i)
        x += move[i];
      else
        y += move[i];
      int xx = 1 << x & a.xh;
      if (!xx || !i) {
        if (x <= w && 0 < x && y <= h) {
          if (stratum[y][x] < 0 && a.cost + stratum[y][x] >= -f &&
              a.cost + stratum[y][x] > c[y][x][a.o - 1]) {
            c[y][x][a.o - 1] = a.cost + stratum[y][x];
            if (i)
              pq.push({c[y][x][a.o - 1], x, y, a.o - 1, a.xh | 1 << x});
            else
              pq.push({c[y][x][a.o - 1], x, y, a.o - 1, 1 << x});
          } else if (stratum[y][x] > 0) {
            int hokyuuu;
            hokyuuu = stratum[y][x] + a.o - 1;
            if (hokyuuu + a.o - 1 >= m)
              hokyuuu = m;
            c[y][x][hokyuuu] = a.cost;
            if (i)
              pq.push({c[y][x][hokyuuu], x, y, hokyuuu, a.xh | 1 << x});
            else
              pq.push({c[y][x][hokyuuu], x, y, hokyuuu, 1 << x});
          } else {
            c[y][x][a.o] = a.cost + stratum[y][x];
            if (i)
              pq.push({c[y][x][a.o], x, y, a.o, a.xh | 1 << x});
            else
              pq.push({c[y][x][a.o], x, y, a.o, 1 << x});
          }
        }
      } else {
        if (a.cost > c[y][x][a.o - 1] && a.o - 1) {
          c[y][x][a.o - 1] = a.cost;
          pq.push({c[y][x][a.o - 1], x, y, a.o - 1, a.xh});
        }
      }
    }
  }
}

int main() {
  while (cin >> w >> h && w && h) {
    cin >> f >> m >> o;
    memset(stratum, sizeof(stratum), 0);
    ans = INF2;
    for (int i = 1; i <= 10; i++)
      for (int j = 1; j <= 10; j++)
        fill(c[i][j], c[i][j] + 51, INF);
    for (int i = 1; i <= h; i++)
      for (int j = 1; j <= w; j++)
        cin >> stratum[i][j];
    for (int i = 1; i <= w; i++) {
      c[1][i][o - 1] = stratum[1][i];
      pq.push({stratum[1][i], i, 1, o - 1, 1 << i});
    }
    dijkstra();
    for (int i = 1; i <= m; i++)
      for (int j = 1; j <= w; j++)
        if (c[h][j][i] != INF)
          ans = min(ans, -c[h][j][i]);
    if (ans != INF2)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}