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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

struct edge {
  int to, cost, enemy;
};

struct P {
  int from, money, enemy;
  bool operator>(const P &p) const { return enemy > p.enemy; }
};

int main() {
  int N, M, L;
  while (cin >> N >> M >> L, N | M | L) {
    vector<vector<edge>> E = vector<vector<edge>>(N + 1);
    REP(i, M) {
      int from, to, cost, enemy;
      cin >> from >> to >> cost >> enemy;
      E[from].push_back((edge){to, cost, enemy});
      E[to].push_back((edge){from, cost, enemy});
    }

    priority_queue<P, vector<P>, greater<P>> que;
    vector<int> costs = vector<int>(N + 1);
    REP(i, costs.size()) { costs[i] = -1; }

    que.push((P){1, L, 0});
    int res = -1;
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      if (p.money <= costs[p.from])
        continue;
      costs[p.from] = p.money;
      if (p.from == N) {
        res = p.enemy;
        break;
      }
      REP(i, E[p.from].size()) {
        edge e = E[p.from][i];
        if (p.money >= e.cost) {
          que.push((P){e.to, p.money - e.cost, p.enemy});
        }
        que.push((P){e.to, p.money, p.enemy + e.enemy});
      }
    }

    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

struct edge {
  int to, cost, enemy;
};

struct P {
  int from, money, enemy;
  bool operator>(const P &p) const { return enemy > p.enemy; }
};

int main() {
  int N, M, L;
  while (cin >> N >> M >> L, N | M | L) {
    vector<vector<edge>> E = vector<vector<edge>>(N + 1);
    REP(i, M) {
      int from, to, cost, enemy;
      cin >> from >> to >> cost >> enemy;
      E[from].push_back((edge){to, cost, enemy});
      E[to].push_back((edge){from, cost, enemy});
    }

    priority_queue<P, vector<P>, greater<P>> que;
    vector<int> costs = vector<int>(N + 1);
    REP(i, costs.size()) { costs[i] = INT_MAX; }

    que.push((P){1, L, 0});
    int res = -1;
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      if (p.from == N) {
        res = p.enemy;
        break;
      }
      if (p.enemy > costs[p.from])
        continue;
      costs[p.from] = p.enemy;
      REP(i, E[p.from].size()) {
        edge e = E[p.from][i];
        if (p.money >= e.cost) {
          que.push((P){e.to, p.money - e.cost, p.enemy});
        }
        que.push((P){e.to, p.money, p.enemy + e.enemy});
      }
    }

    cout << res << endl;
  }
}