Problem p01269 (C++) — buggy submission, verdict: Time 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];
  }
};

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

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, vector<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];
}

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

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

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;
    }

    while (!status.empty()) {
      status.pop();
    }

    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;
              if (points[i].find(e) == points[i].end()) {
                points[i][e] = b;
                status.push(State(i, e, b));
              } else if (points[i][e] < b) {
                points[i][e] = b;
                status.push(State(i, e, b));
              }
            } else {
              if (now.budget - maps[now.where][i] >= 0) {
                e = now.enemy;
                b = now.budget - maps[now.where][i];
                if (points[i].find(e) == points[i].end()) {
                  points[i][e] = b;
                  status.push(State(i, e, b));
                } else if (points[i][e] < b) {
                  points[i][e] = b;
                  status.push(State(i, e, b));
                }
              }
              e = now.enemy + off[now.where][i];
              b = now.budget;
              if (points[i].find(e) == points[i].end()) {
                points[i][e] = b;
                status.push(State(i, e, b));
              } else if (points[i][e] < b) {
                points[i][e] = b;
                status.push(State(i, e, b));
              }
            }
          }
        }
      }
    }
  }
}


Error: TLE

Fixed submission:

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

using namespace std;

int map[LEN][LEN];
int off[LEN][LEN];

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

/*
class sState {
        int where
*/
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];
  }
};

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

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, vector<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];
}

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

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

int main() {

  while (true) {
    int n, m, l;
    char tab = '\\t';
    cin >> n >> m >> l;
    // cout << "Prob: " << n << tab << m << tab << l << endl;

    if (n == 0)
      break;

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        map[i][j] = -1;
        off[i][j] = 0;
      }
    }

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

      map[a - 1][b - 1] = map[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;
    }

    while (!status.empty()) {
      status.pop();
    }

    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;
      }

      // cout << "E: " << now.enemy << endl;

      copy(now);

      for (int i = 0; i <= n; i++) {
        if (map[now.where][i] > 0) {
          if (check(i)) {
            // cout << "PASSED" << endl;
            // cout << now.where+1 << tab << i+1 << endl;
            ncopy();
            add(i);
            if (off[now.where][i] == 0) {
              status.push(State(i, now.enemy, now.budget));
            } else {
              if (now.budget - map[now.where][i] >= 0) {
                count++;
                status.push(
                    State(i, now.enemy, now.budget - map[now.where][i]));
              }
              status.push(State(i, now.enemy + off[now.where][i], now.budget));
            }
          }
        }
      }
      // cout << "C: " << count << endl;
    }
  }
}