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

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

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

int nextInt() {
  int a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}

void inputVector(vector<int> &v, int &n) {
  rep(i, n) { v.push_back(nextInt()); }
}
void inputVector(vector<double> &v, int &n) {
  rep(i, n) { v.push_back(nextDouble()); }
}
void inputVector(vector<string> &v, int &n) {
  rep(i, n) { v.push_back(nextString()); }
}

class Edge {
public:
  int next;
  int dist;
  int weight;
  Edge() {}
  Edge(int n, int d, int w) {
    next = n;
    dist = d;
    weight = w;
  }
};

class Info {
public:
  int node;
  int dist;
  int left;
  Info(int n, int d, int l) {
    node = n;
    dist = d;
    left = l;
  }
  bool operator<(const Info &a) const {
    if (dist == a.dist && node == a.node)
      return left < a.left;
    return dist > a.dist;
  }
};

int dist[100][110];
vector<vector<Edge>> nexts;

signed main() {
  int N, M, L;
  while (cin >> N >> M >> L) {
    if (N == 0)
      break;

    nexts.clear();
    nexts.resize(N);
    rep(i, 100) rep(j, 101) dist[i][j] = INT_MAX;

    int A, B, D, E;
    rep(i, M) {
      cin >> A >> B >> D >> E;
      A--;
      B--;
      nexts[A].push_back(Edge(B, D, E));
      nexts[B].push_back(Edge(A, D, E));
    }

    priority_queue<Info> q;
    q.push(Info(0, 0, L));
    rep(i, 101) dist[0][i] = 0;
    while (!q.empty()) {
      Info info = q.top();
      q.pop();
      if (info.dist > dist[info.node][info.left])
        continue;
      if (info.node == N - 1) {
        cout << info.dist << endl;
        break;
      }
      for (Edge &edge : nexts[info.node]) {
        if (info.left >= edge.dist && edge.weight > 0) {
          if (dist[edge.next][info.left - edge.dist] > info.dist) {
            rep(i, info.left - edge.dist) {
              if (info.dist >= dist[edge.next][i] - 1)
                break;
              dist[edge.next][i] = info.dist - 1;
            }
            dist[edge.next][info.left - edge.dist] = info.dist;
            q.push(Info(edge.next, info.dist, info.left - edge.dist));
          }
        }
        int ndist = info.dist + edge.weight;
        if (dist[edge.next][info.left] > ndist) {
          rep(i, info.left) {
            if (dist[edge.next][i] >= ndist - 1)
              break;
            dist[edge.next][i] = ndist - 1;
          }
          dist[edge.next][info.left] = ndist;
          q.push(Info(edge.next, ndist, info.left));
        }
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

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

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

int nextInt() {
  int a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}

void inputVector(vector<int> &v, int &n) {
  rep(i, n) { v.push_back(nextInt()); }
}
void inputVector(vector<double> &v, int &n) {
  rep(i, n) { v.push_back(nextDouble()); }
}
void inputVector(vector<string> &v, int &n) {
  rep(i, n) { v.push_back(nextString()); }
}

class Edge {
public:
  int next;
  int dist;
  int weight;
  Edge() {}
  Edge(int n, int d, int w) {
    next = n;
    dist = d;
    weight = w;
  }
};

class Info {
public:
  int node;
  int dist;
  int left;
  Info(int n, int d, int l) {
    node = n;
    dist = d;
    left = l;
  }
  bool operator<(const Info &a) const {
    if (dist == a.dist && node == a.node)
      return left < a.left;
    return dist > a.dist;
  }
};

int dist[100][110];
vector<vector<Edge>> nexts;

signed main() {
  int N, M, L;
  while (cin >> N >> M >> L) {
    if (N == 0)
      break;

    nexts.clear();
    nexts.resize(N);
    rep(i, 100) rep(j, 101) dist[i][j] = INT_MAX;

    int A, B, D, E;
    rep(i, M) {
      cin >> A >> B >> D >> E;
      A--;
      B--;
      nexts[A].push_back(Edge(B, D, E));
      nexts[B].push_back(Edge(A, D, E));
    }

    priority_queue<Info> q;
    q.push(Info(0, 0, L));
    rep(i, 101) dist[0][i] = 0;
    while (!q.empty()) {
      Info info = q.top();
      q.pop();
      if (info.dist > dist[info.node][info.left])
        continue;
      if (info.node == N - 1) {
        cout << info.dist << endl;
        break;
      }
      for (Edge &edge : nexts[info.node]) {
        if (info.left >= edge.dist && edge.weight > 0) {
          if (dist[edge.next][info.left - edge.dist] > info.dist) {
            REP(i, 0, info.left - edge.dist) {
              if (info.dist >= dist[edge.next][i])
                break;
              dist[edge.next][i] = info.dist;
            }
            q.push(Info(edge.next, info.dist, info.left - edge.dist));
          }
        }
        int ndist = info.dist + edge.weight;
        if (dist[edge.next][info.left] > ndist) {
          REP(i, 0, info.left) {
            if (dist[edge.next][i] >= ndist)
              break;
            dist[edge.next][i] = ndist;
          }
          q.push(Info(edge.next, ndist, info.left));
        }
      }
    }
  }

  return 0;
}