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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct inn {
  int n, c, r;
  inn(int n, int c, int r) : n(n), c(c), r(r) {}
  bool operator<(const inn &other) const { return r < other.r; }
};
const int MAX = INT_MAX / 10;
int main() {
  int n, m, l;
  while (cin >> n >> m >> l, n) {
    vvi dist(n, vi(n, -1));
    vvi risk(n, vi(n, MAX));
    REP(i, m) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      dist[a - 1][b - 1] = d;
      dist[b - 1][a - 1] = d;
      risk[a - 1][b - 1] = e;
      risk[b - 1][a - 1] = e;
    }

    queue<inn> q;
    q.push(inn(0, 0, 0));
    vvi cost(n, vi(l + 1, MAX));
    cost[0][0] = 0;
    while (!q.empty()) {
      inn in = q.front();
      q.pop();
      if (cost[in.n][in.c] < in.r) {
        continue;
      }
      REP(i, n) {
        if (i != in.n && dist[in.n][i] != -1) {
          REP(j, 2) {
            int nr = in.r + (j == 1 ? 0 : risk[in.n][i]);
            int nc = in.c + (j == 0 ? 0 : dist[in.n][i]);
            if (nc <= l && cost[i][nc] > nr) {
              cost[i][nc] = nr;
              q.push(inn(i, nc, nr));
            }
          }
        }
      }
    }
    int best = INT_MAX;
    REP(i, l + 1) { best = min(best, cost[n - 1][i]); }
    cout << best << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct inn {
  int n, c, r;
  inn(int n, int c, int r) : n(n), c(c), r(r) {}
  bool operator<(const inn &other) const { return r < other.r; }
};
const int MAX = INT_MAX / 10;
int main() {
  int n, m, l;
  while (cin >> n >> m >> l, n) {
    vvi dist(n, vi(n, -1));
    vvi risk(n, vi(n, MAX));
    REP(i, m) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      dist[a - 1][b - 1] = d;
      dist[b - 1][a - 1] = d;
      risk[a - 1][b - 1] = e;
      risk[b - 1][a - 1] = e;
    }

    priority_queue<inn> q;
    q.push(inn(0, 0, 0));
    vvi cost(n, vi(l + 1, MAX));
    cost[0][0] = 0;
    while (!q.empty()) {
      inn in = q.top();
      q.pop();
      if (cost[in.n][in.c] < in.r) {
        continue;
      }
      REP(i, n) {
        if (i != in.n && dist[in.n][i] != -1) {
          REP(j, 2) {
            int nr = in.r + (j == 1 ? 0 : risk[in.n][i]);
            int nc = in.c + (j == 0 ? 0 : dist[in.n][i]);
            if (nc <= l && cost[i][nc] > nr) {
              cost[i][nc] = nr;
              q.push(inn(i, nc, nr));
            }
          }
        }
      }
    }
    int best = INT_MAX;
    REP(i, l + 1) { best = min(best, cost[n - 1][i]); }
    cout << best << endl;
  }
}