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

#include <iostream>
#include <map>
#include <queue>
#define LEN 100
#define BASE 25

using namespace std;

int maps[LEN][LEN];
int off[LEN][LEN];

int checker[4];
int ncheck[4];

map<int, int> points[LEN];

class State {
public:
  char where;
  int enemy;
  int budget;
  int check[4];
  State() {
    check[0] = 0;
    check[1] = 0;
    check[2] = 0;
    check[3] = 0;
  }
  State(char w, int e, int b) {
    where = w;
    enemy = e;
    budget = b;
    check[0] = ncheck[0];
    check[1] = ncheck[1];
    check[2] = ncheck[2];
    check[3] = ncheck[3];
  }
};

inline bool operator<(const State &a, const State &b) {
  if (a.enemy == b.enemy)
    return a.budget > b.budget;
  return a.enemy < b.enemy;
}

inline bool operator>(const State &a, const State &b) {
  if (a.enemy == b.enemy)
    return a.budget < b.budget;
  return a.enemy > b.enemy;
}

priority_queue<State, deque<State>, greater<State>> status;

inline void copy(State now) {
  checker[0] = now.check[0];
  checker[1] = now.check[1];
  checker[2] = now.check[2];
  checker[3] = now.check[3];
}

inline void ncopy() {
  ncheck[0] = checker[0];
  ncheck[1] = checker[1];
  ncheck[2] = checker[2];
  ncheck[3] = checker[3];
}

inline bool check(int i) { return 0 == ((1 << (i % 25)) & checker[i / BASE]); }

inline void add(int i) {
  ncheck[i / BASE] = ((1 << (i % 25)) | checker[i / BASE]);
}

inline void insert(int i, int e, int b) {

  map<int, int>::iterator itr = points[i].find(e);
  if (itr == points[i].end()) {
    points[i][e] = b;
    status.push(State(i, e, b));
  } else if ((*itr).second < b) {
    (*itr).second = b;
    status.push(State(i, e, b));
  }

  itr++;
  int min = b + 1;

  while (itr != points[i].end()) {
    if ((*itr).second > min) {
      (*itr).second = min;
    } else {
      min = (*itr).second + 1;
    }
    itr++;
  }
}

int main() {

  while (true) {
    int n, m, l;
    cin >> n >> m >> l;

    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        maps[i][j] = -1;
        off[i][j] = 0;
      }
      points[i].clear();
    }

    for (int i = 0; i < m; i++) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;

      maps[a - 1][b - 1] = maps[b - 1][a - 1] = d;
      off[a - 1][b - 1] = off[b - 1][a - 1] = e;
    }

    for (int i = 0; i < 4; i++) {
      ncheck[i] = 0;
      checker[i] = 0;
    }

    priority_queue<State, deque<State>, greater<State>> empty;
    status = empty;

    ncheck[0] = 1;
    status.push(State(0, 0, l));
    int count = 0;
    while (!status.empty()) {
      State now = status.top();
      status.pop();
      if (now.where == n - 1) {
        cout << now.enemy << endl;
        break;
      }

      copy(now);

      for (int i = 0; i < n; i++) {
        if (maps[now.where][i] > 0) {
          if (check(i)) {
            ncopy();
            add(i);
            int e, b;
            if (off[now.where][i] == 0) {
              e = now.enemy;
              b = now.budget;
              insert(i, e, b);
            } else {
              if (now.budget - maps[now.where][i] >= 0) {
                e = now.enemy;
                b = now.budget - maps[now.where][i];
                insert(i, e, b);
              }
              e = now.enemy + off[now.where][i];
              b = now.budget;
              insert(i, e, b);
            }
          }
        }
      }
    }
  }
}


Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#define LEN 100
#define BASE 25

using namespace std;

int maps[LEN][LEN];
int off[LEN][LEN];

int checker[4];
int ncheck[4];

map<int, int> points[LEN];

class State {
public:
  char where;
  int enemy;
  int budget;
  int check[4];
  State() {
    check[0] = 0;
    check[1] = 0;
    check[2] = 0;
    check[3] = 0;
  }
  State(char w, int e, int b) {
    where = w;
    enemy = e;
    budget = b;
    check[0] = ncheck[0];
    check[1] = ncheck[1];
    check[2] = ncheck[2];
    check[3] = ncheck[3];
  }
};

inline bool operator<(const State &a, const State &b) {
  if (a.enemy == b.enemy)
    return a.budget > b.budget;
  return a.enemy < b.enemy;
}

inline bool operator>(const State &a, const State &b) {
  if (a.enemy == b.enemy)
    return a.budget < b.budget;
  return a.enemy > b.enemy;
}

priority_queue<State, deque<State>, greater<State>> status;

inline void copy(State now) {
  checker[0] = now.check[0];
  checker[1] = now.check[1];
  checker[2] = now.check[2];
  checker[3] = now.check[3];
}

inline void ncopy() {
  ncheck[0] = checker[0];
  ncheck[1] = checker[1];
  ncheck[2] = checker[2];
  ncheck[3] = checker[3];
}

inline bool check(int i) { return 0 == ((1 << (i % 25)) & checker[i / BASE]); }

inline void add(int i) {
  ncheck[i / BASE] = ((1 << (i % 25)) | checker[i / BASE]);
}

inline void insert(int i, int e, int b) {

  map<int, int>::iterator itr = points[i].find(e);
  if (itr == points[i].end()) {
    points[i][e] = b;
    status.push(State(i, e, b));
  } else if ((*itr).second < b) {
    (*itr).second = b;
    status.push(State(i, e, b));
  }

  int min = b + 1;

  while (itr != points[i].end()) {
    if ((*itr).second > min) {
      (*itr).second = min;
    } else {
      min = (*itr).second + 1;
    }
    itr++;
  }
}

int main() {

  while (true) {
    int n, m, l;
    cin >> n >> m >> l;

    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        maps[i][j] = -1;
        off[i][j] = 0;
      }
      points[i].clear();
    }

    for (int i = 0; i < m; i++) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;

      maps[a - 1][b - 1] = maps[b - 1][a - 1] = d;
      off[a - 1][b - 1] = off[b - 1][a - 1] = e;
    }

    for (int i = 0; i < 4; i++) {
      ncheck[i] = 0;
      checker[i] = 0;
    }

    priority_queue<State, deque<State>, greater<State>> empty;
    status = empty;

    ncheck[0] = 1;
    status.push(State(0, 0, l));
    int count = 0;
    while (!status.empty()) {
      State now = status.top();
      status.pop();
      if (now.where == n - 1) {
        cout << now.enemy << endl;
        break;
      }

      copy(now);

      for (int i = 0; i < n; i++) {
        if (maps[now.where][i] > 0) {
          if (check(i)) {
            ncopy();
            add(i);
            int e, b;
            if (off[now.where][i] == 0) {
              e = now.enemy;
              b = now.budget;
              insert(i, e, b);
            } else {
              if (now.budget - maps[now.where][i] >= 0) {
                e = now.enemy;
                b = now.budget - maps[now.where][i];
                insert(i, e, b);
              }
              e = now.enemy + off[now.where][i];
              b = now.budget;
              insert(i, e, b);
            }
          }
        }
      }
    }
  }
}