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

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

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

// aoj 2151
struct Edge {
  int to, cost, enemy;
  Edge(){};
  Edge(int to, int cost, int enemy) : to(to), cost(cost), enemy(enemy){};
};

vector<Edge> edge[110];
int dist[110][110];

int main() {
  while (1) {
    int n = in.nextInt(), m = in.nextInt(), l = in.nextInt();
    if (n == 0 && m == 0 && l == 0)
      break;
    rep(i, 110) edge[i].clear();
    rep(i, 110) rep(j, 110) dist[i][j] = i == 0 ? 0 : INF;

    rep(i, m) {
      int a = in.nextInt() - 1, b = in.nextInt() - 1, d = in.nextInt(),
          e = in.nextInt();
      edge[a].pb({b, d, e});
      edge[b].pb({a, d, e});
    }

    auto dijkstra = [&] {
      typedef pair<int, pii> P;
      priority_queue<P, vector<P>, greater<P>> q;
      q.push({0, {0, l}});
      P p;
      while (!q.empty()) {
        p = q.top();
        q.pop();
        int cost = p.fi, from = p.se.fi, mny = p.se.se;
        for (auto e : edge[from]) {
          int to = e.to, c = e.cost, enmy = e.enemy;
          if (dist[to][mny] > cost + enmy) {
            dist[to][mny] = cost + enmy;
            q.push({cost + enmy, {to, mny}});
          }
          if (mny < c)
            continue;
          if (dist[to][mny - c] > cost) {
            dist[to][mny - c] = cost;
            q.push({cost, {to, mny - c}});
          }
        }
      }
    };
    dijkstra();

    int ans = INF;
    rep(i, l + 1) ans = min(ans, dist[n - 1][i]);
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

int n, m, l;

int d[110][110][110];

int dist[110][110];

int main() {
  while (1) {
    n = in.nextInt();
    m = in.nextInt();
    l = in.nextInt();
    if (n == 0 && m == 0 && l == 0)
      break;
    rep(i, 110) rep(j, 110) rep(k, 110) d[i][j][k] = (i == j ? 0 : INF);
    rep(i, 110) rep(j, 110) dist[i][j] = INF;

    rep(i, m) {
      int a = in.nextInt() - 1, b = in.nextInt() - 1, c = in.nextInt(),
          e = in.nextInt();
      d[a][b][l] = d[b][a][l] = e;
      dist[a][b] = dist[b][a] = c;
    }

    for (int money = l; money >= 0; money--) {
      rep(k, 110) {
        rep(i, 110) {
          rep(j, 110) {
            d[i][j][money] = min(
                {d[i][j][money + 1], d[i][j][money], d[i][k][l] + d[k][j][l]});
            if (dist[i][k] <= money)
              d[i][j][money - dist[i][k]] =
                  min(d[i][j][money - dist[i][k]], d[k][j][l]);
            if (dist[k][j] <= money)
              d[i][j][money - dist[j][k]] =
                  min(d[i][j][money - dist[j][k]], d[i][k][l]);
            if (dist[i][k] + dist[k][j] <= money)
              d[i][j][money - (dist[i][k] + dist[k][j])] = 0;
          }
        }
      }
    }
    int ans = INF;
    rep(i, l + 1) ans = min(ans, d[0][n - 1][i]);

    printf("%d\\n", ans);
  }
}