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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int n;
int dist[101][101];
int atk[101][101];
int memo[101][101]; // memo[i][j] :
                    // ??????j??§?????????i???????????¨??????????°??\\???\\?????°
bool used[101][101];
const int INF = 1000000000;

struct V {
  int p, m;
  V(int p, int m) : p(p), m(m) {}
};

bool operator<(const V &a, const V &b) {
  return memo[a.p][a.m] > memo[b.p][b.m];
}

int solve(int s, int l) {
  for (int i = 0; i <= 100; ++i)
    for (int j = 0; j <= 100; ++j) {
      used[i][j] = false;
      memo[i][j] = INF;
    }

  memo[s][l] = 0;
  priority_queue<V> Q;
  Q.push(V(s, l));

  while (!Q.empty()) {
    V x = Q.top();
    Q.pop();
    int cur = x.p, mon = x.m, cst = memo[cur][mon];
    //    if(used[cur][mon]) continue;
    //    used[cur][mon] = true;
    for (int i = 0; i < n; ++i) {
      if (dist[cur][i]) {
        if (cst + atk[cur][i] < memo[i][mon]) {
          memo[i][mon] = cst + atk[cur][i];
          Q.push(V(i, mon));
        }
        if (mon - dist[cur][i] >= 0) {
          if (memo[i][mon - dist[cur][i]] > cst) {
            memo[i][mon - dist[cur][i]] = cst;
            Q.push(V(i, mon - dist[cur][i]));
          }
        }
      }
    }
  }

  int ret = INF;
  for (int i = 0; i <= l; ++i)
    ret = min(ret, memo[n - 1][i]);
  return ret;
}
int main() {
  int m, l;
  while (cin >> n >> m >> l, n || m || l) {
    memset(dist, 0, sizeof(dist));
    memset(atk, 0, sizeof(atk));
    while (m--) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--;
      b--;
      dist[a][b] = dist[b][a] = d;
      atk[a][b] = atk[b][a] = e;
    }
    cout << solve(0, l) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int n;
int dist[101][101];
int atk[101][101];
int memo[101][101]; // memo[i][j] :
                    // ??????j??§?????????i???????????¨??????????°??\\???\\?????°
bool used[101][101];
const int INF = 1000000000;

struct V {
  int p, m;
  V(int p, int m) : p(p), m(m) {}
};

bool operator<(const V &a, const V &b) {
  return memo[a.p][a.m] < memo[b.p][b.m];
}

int solve(int s, int l) {
  for (int i = 0; i <= 100; ++i)
    for (int j = 0; j <= 100; ++j) {
      used[i][j] = false;
      memo[i][j] = INF;
    }

  memo[s][l] = 0;
  priority_queue<V> Q;
  Q.push(V(s, l));

  while (!Q.empty()) {
    V x = Q.top();
    Q.pop();
    int cur = x.p, mon = x.m, cst = memo[cur][mon];
    for (int i = 0; i < n; ++i) {
      if (dist[cur][i]) {
        if (cst + atk[cur][i] < memo[i][mon]) {
          memo[i][mon] = cst + atk[cur][i];
          Q.push(V(i, mon));
        }
        if (mon - dist[cur][i] >= 0) {
          if (memo[i][mon - dist[cur][i]] > cst) {
            memo[i][mon - dist[cur][i]] = cst;
            Q.push(V(i, mon - dist[cur][i]));
          }
        }
      }
    }
  }

  int ret = INF;
  for (int i = 0; i <= l; ++i)
    ret = min(ret, memo[n - 1][i]);
  return ret;
}
int main() {
  int m, l;
  while (cin >> n >> m >> l, n || m || l) {
    memset(dist, 0, sizeof(dist));
    memset(atk, 0, sizeof(atk));
    while (m--) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--;
      b--;
      dist[a][b] = dist[b][a] = d;
      atk[a][b] = atk[b][a] = e;
    }
    cout << solve(0, l) << endl;
  }
  return 0;
}