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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> vector<T> &sort(vector<T> &a) {
  sort(all(a));
  return a;
}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int N, M, L;

struct State {
  int v, sum, dist;
  State(int v, int sum, int dist) : v(v), sum(sum), dist(dist) {}
  bool operator<(const State &a) const { return dist > a.dist; }
};

struct edge {
  int to, money, enemy;
  edge(int to, int money, int enemy) : to(to), money(money), enemy(enemy) {}
};

vector<edge> G[101];
int d[101][101];

void dijkstra(int s) {
  for (int i = 0; i <= 100; ++i)
    for (int j = 0; j < N; ++j)
      d[i][j] = inf;

  priority_queue<State> q;
  q.push(State(s, 0, 0));

  d[0][s] = 0;

  while (q.size()) {
    State st = q.top();
    q.pop();

    if (d[st.sum][st.v] < st.dist)
      continue;

    for (int i = 0; i < G[st.v].size(); ++i) {
      edge e = G[st.v][i];

      if (st.sum + e.money <= L && d[st.sum + e.money][e.to] > st.dist) {
        d[st.sum + e.money][e.to] = st.dist;
        q.push(State(e.to, st.sum + e.money, d[st.sum + e.money][e.to]));
      }

      if (d[st.sum][e.to] > st.dist + e.enemy) {
        d[st.sum][e.to] = st.dist + e.enemy;
        q.push(State(e.to, st.sum, d[st.sum][e.to]));
      }
    }
  }
}

signed main() {
  while (scanf("%d%d%d", &N, &M, &L), N) {
    for (int i = 0; i < N; ++i)
      G[i].clear();

    for (int i = 0; i < M; ++i) {
      int A, B, D, E;
      scanf("%d%d%d%d", &A, &B, &D, &E);
      A--, B--;
      G[A].push_back(edge(B, D, E));
      G[B].push_back(edge(A, D, E));
    }

    dijkstra(0);

    int mi = inf;

    for (int i = 0; i <= 100; ++i) {
      chmin(mi, d[i][N - 1]);
    }

    printf("%d\\n", mi);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T> T rev(T a) {
  reverse(all(a));
  return a;
}
template <typename T> vector<T> &sort(vector<T> &a) {
  sort(all(a));
  return a;
}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int N, M, L;

struct State {
  int v, sum, dist;
  State(int v, int sum, int dist) : v(v), sum(sum), dist(dist) {}
  bool operator<(const State &a) const { return dist > a.dist; }
};

struct edge {
  int to, money, enemy;
  edge(int to, int money, int enemy) : to(to), money(money), enemy(enemy) {}
};

vector<edge> G[101];
int d[101][101];

void dijkstra(int s) {
  for (int i = 0; i <= 100; ++i)
    for (int j = 0; j < N; ++j)
      d[i][j] = inf;

  priority_queue<State> q;
  q.push(State(s, 0, 0));

  d[0][s] = 0;

  while (q.size()) {
    State st = q.top();
    q.pop();

    if (d[st.sum][st.v] < st.dist)
      continue;

    for (int i = 0; i < G[st.v].size(); ++i) {
      edge e = G[st.v][i];

      do {
        if (st.sum + e.money <= L) {
          if (d[st.sum + e.money][e.to] < st.dist)
            continue;
          d[st.sum + e.money][e.to] = st.dist;
          q.push(State(e.to, st.sum + e.money, d[st.sum + e.money][e.to]));
        }
      } while (0);

      if (d[st.sum][e.to] < st.dist + e.enemy)
        continue;
      d[st.sum][e.to] = st.dist + e.enemy;
      q.push(State(e.to, st.sum, d[st.sum][e.to]));
    }
  }
}

signed main() {
  while (scanf("%d%d%d", &N, &M, &L), N) {
    for (int i = 0; i < N; ++i)
      G[i].clear();

    for (int i = 0; i < M; ++i) {
      int A, B, D, E;
      scanf("%d%d%d%d", &A, &B, &D, &E);
      A--, B--;
      G[A].push_back(edge(B, D, E));
      G[B].push_back(edge(A, D, E));
    }

    dijkstra(0);

    int mi = inf;

    for (int i = 0; i <= 100; ++i) {
      chmin(mi, d[i][N - 1]);
    }

    printf("%d\\n", mi);
  }
}