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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, n) for (int i = s; i < n; i++)
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

pii cost[101][101];
int visit[101];

struct node {
  int cost, money, pos;
  node(int c, int m, int p) {
    cost = c;
    money = m;
    pos = p;
  }
};
bool operator<(const node &l, const node &r) {
  if (l.cost == r.cost) {
    return l.money < r.money;
  }
  return l.cost > r.cost;
}

int main() {
  int n, m, l;
  while (cin >> n >> m >> l, n || m || l) {
    rep(i, 101) rep(j, 101) cost[i][j].first = cost[i][j].second = -1;
    rep(i, m) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      cost[a][b].first = cost[b][a].first = d;
      cost[a][b].second = cost[b][a].second = e;
    }
    priority_queue<node> que;
    memset(visit, -1, sizeof(visit));
    que.push(node(0, l, 1));
    while (!que.empty()) {
      node nd = que.top();
      que.pop();
      if (nd.money <= visit[nd.pos])
        continue;
      visit[nd.pos] = nd.money;
      if (nd.pos == n) {
        cout << nd.cost << endl;
        break;
      }
      for (int i = 1; i <= n; i++) {
        if (cost[nd.pos][i].second != -1) {
          if (visit[i] < nd.money) {
            que.push(node(nd.cost + cost[nd.pos][i].second, nd.money, i));
          }
          if (nd.money >= cost[nd.pos][i].first &&
              visit[i] < nd.money - cost[nd.pos][i].first) {
            que.push(node(nd.cost, nd.money - cost[nd.pos][i].first, i));
          }
        }
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, n) for (int i = s; i < n; i++)
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

pii cost[101][101];
bool visit[101][101];

struct node {
  int cost, money, pos;
  node(int c, int m, int p) {
    cost = c;
    money = m;
    pos = p;
  }
};
bool operator<(const node &l, const node &r) {
  if (l.cost == r.cost) {
    return l.money < r.money;
  }
  return l.cost > r.cost;
}

int main() {
  int n, m, l;
  while (cin >> n >> m >> l, n || m || l) {
    rep(i, 101) rep(j, 101) cost[i][j].first = cost[i][j].second = -1;
    rep(i, m) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      cost[a][b].first = cost[b][a].first = d;
      cost[a][b].second = cost[b][a].second = e;
    }
    priority_queue<node> que;
    memset(visit, 0, sizeof(visit));
    que.push(node(0, l, 1));
    while (!que.empty()) {
      node nd = que.top();
      que.pop();
      if (visit[nd.pos][nd.money])
        continue;
      visit[nd.pos][nd.cost] = true;
      if (nd.pos == n) {
        cout << nd.cost << endl;
        break;
      }
      for (int i = 1; i <= n; i++) {
        if (cost[nd.pos][i].second != -1) {
          if (!visit[i][nd.money]) {
            que.push(node(nd.cost + cost[nd.pos][i].second, nd.money, i));
          }
          if (nd.money >= cost[nd.pos][i].first &&
              !visit[i][nd.money - cost[nd.pos][i].first]) {
            que.push(node(nd.cost, nd.money - cost[nd.pos][i].first, i));
          }
        }
      }
    }
  }
  return 0;
}