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

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> istream &operator>>(istream &is, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, vector<T> const &v);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  const tuple<T...> &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 const tuple<T...> &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &t) {
  _ot<0>(os, t);
  return os;
}
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _it(istream &, tuple<T...> &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _it(istream &is, tuple<T...> &t) {
  is >> get<n>(t);
  _it<n + 1>(is, t);
}
template <class... T> istream &operator>>(istream &is, tuple<T...> &t) {
  _it<0>(is, t);
  return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  rep(i, v.size()) is >> v[i];
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif

// グラフ用ヘッダ

struct Edge {
  int src, dst;
  int weight, money;
  Edge(int src, int dst, int weight, int money)
      : src(src), dst(dst), weight(weight), money(money) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int dp[111][111];

int main() {
  int N, M, L;
  while (cin >> N >> M >> L && N) {
    Graph g(N);
    rep(i, M) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--;
      b--;
      swap(d, e);
      g[a].eb(a, b, d, e);
      g[b].eb(b, a, d, e);
    }
    rep(i, 111) rep(j, 111) dp[i][j] = 1 << 29;
    dp[0][L] = 0;
    typedef tuple<int, int, int> State;
    priority_queue<State> q;
    q.emplace(0, 0, L);
    while (q.size()) {
      int c, v, money;
      tie(c, v, money) = q.top();
      q.pop();
      c = -c;
      if (dp[v][money] < c)
        continue;
      for (auto &e : g[v]) {
        if (dp[v][money] + e.weight < dp[e.dst][money]) {
          dp[e.dst][money] = dp[v][money] + e.weight;
          q.emplace(-dp[v][money] + e.weight, e.dst, money);
        }
        if (money >= e.money && dp[v][money] < dp[e.dst][money - e.money]) {
          dp[e.dst][money - e.money] = dp[v][money];
          q.emplace(-dp[v][money], e.dst, money - e.money);
        }
      }
    }
    int ans = dp[N - 1][0];
    rep(i, 111) ans = min(ans, dp[N - 1][i]);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> istream &operator>>(istream &is, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, vector<T> const &v);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  const tuple<T...> &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 const tuple<T...> &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &t) {
  _ot<0>(os, t);
  return os;
}
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _it(istream &, tuple<T...> &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _it(istream &is, tuple<T...> &t) {
  is >> get<n>(t);
  _it<n + 1>(is, t);
}
template <class... T> istream &operator>>(istream &is, tuple<T...> &t) {
  _it<0>(is, t);
  return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  rep(i, v.size()) is >> v[i];
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif

// グラフ用ヘッダ

struct Edge {
  int src, dst;
  int weight, money;
  Edge(int src, int dst, int weight, int money)
      : src(src), dst(dst), weight(weight), money(money) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int dp[111][111];

int main() {
  int N, M, L;
  while (cin >> N >> M >> L && N) {
    Graph g(N);
    rep(i, M) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--;
      b--;
      swap(d, e);
      g[a].eb(a, b, d, e);
      g[b].eb(b, a, d, e);
    }
    rep(i, 111) rep(j, 111) dp[i][j] = 1 << 29;
    dp[0][L] = 0;
    typedef tuple<int, int, int> State;
    priority_queue<State> q;
    q.emplace(0, 0, L);
    while (q.size()) {
      int c, v, money;
      tie(c, v, money) = q.top();
      q.pop();
      c = -c;
      if (dp[v][money] < c)
        continue;
      for (auto &e : g[v]) {
        if (dp[v][money] + e.weight < dp[e.dst][money]) {
          dp[e.dst][money] = dp[v][money] + e.weight;
          q.emplace(dp[v][money] + e.weight, e.dst, money);
        }
        if (money >= e.money && dp[v][money] < dp[e.dst][money - e.money]) {
          dp[e.dst][money - e.money] = dp[v][money];
          q.emplace(dp[v][money], e.dst, money - e.money);
        }
      }
    }
    int ans = dp[N - 1][0];
    rep(i, 111) ans = min(ans, dp[N - 1][i]);
    cout << ans << endl;
  }
}