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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
struct edge {
  int to, d, n;
  edge() {}
  edge(int to, int d, int n) : to(to), d(d), n(n) {}
};
struct S {
  int n, p, l;
  S(int n, int p, int l) : n(n), p(p), l(l) {}
};
bool operator<(const S &s, const S &t) { return s.n > t.n; }
int main() {
  int N, M, L;
  while (cin >> N >> M >> L && N) {
    vector<edge> edges[101];
    REP(i, M) {
      int A, B, D, E;
      cin >> A >> B >> D >> E;
      A--;
      B--;
      edges[A].push_back(edge(B, D, E));
      edges[B].push_back(edge(A, D, E));
    }
    priority_queue<S> que;
    que.push(S(0, 0, L));
    int memo[102][102];
    REP(i, N) REP(j, L + 1) memo[i][j] = INF;
    int ans = INF;
    while (!que.empty()) {
      S s = que.top();
      que.pop();
      if (s.p == N - 1) {
        ans = s.n;
        break;
      }
      if (memo[s.p][s.l] <= s.n)
        continue;
      memo[s.p][s.l] = s.n;
      REP(i, edges[s.p].size()) {
        edge e = edges[s.p][i];
        que.push(S(s.n + e.n, e.to, s.l));
        if (s.l >= e.d)
          que.push(S(s.n, e.to, s.l - e.d));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int N, M, L;
struct edge {
  int to, d, n;
  edge() {}
  edge(int to, int d, int n) : to(to), d(d), n(n) {}
};
vector<edge> edges[101];
int memo[101][101];
int dfs(int p, int l, int sum) {
  // printf("%d %d -> %d\\n", p, l, sum);
  if (p == N - 1)
    return sum;
  memo[p][l] = min((memo[p][l] != -1) ? memo[p][l] : INF, sum);
  int res = INF;
  FORIT(it, edges[p]) {
    edge e = *it;
    if (memo[e.to][l] == -1 || memo[e.to][l] > sum + e.n && res > sum + e.n)
      res = min(res, dfs(e.to, l, sum + e.n));
    if (l - e.d >= 0 &&
        (memo[e.to][l - e.d] == -1 || memo[e.to][l - e.d] > sum && res > sum))
      res = min(res, dfs(e.to, l - e.d, sum));
  }
  return res;
}

int main() {
  while (cin >> N >> M >> L && N) {
    REP(i, 101) edges[i].clear();
    memset(memo, -1, sizeof(memo));
    REP(i, M) {
      int A, B, D, E;
      cin >> A >> B >> D >> E;
      A--;
      B--;
      edges[A].push_back(edge(B, D, E));
      edges[B].push_back(edge(A, D, E));
    }
    int ans = dfs(0, L, 0);
    cout << ans << endl;
  }
  return 0;
}