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

#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define MAX 100000000
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P;
// G@ê@à
struct edge {
  int to, cost, enem;
};
int main() {
  int n, m, l;
  while (cin >> n >> m >> l, n || m || l) {
    vector<edge> tab[110];
    for (int i = 0; i < m; i++) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--;
      b--;
      edge pre;
      pre.cost = d;
      pre.enem = e;
      pre.to = b;
      tab[a].push_back(pre);
      pre.to = a;
      tab[b].push_back(pre);
    }
    int d[110][110];
    // d[ê][à]=G;
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= l; j++) {
        d[i][j] = MAX;
      }
    d[0][l] = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, p(0, l)));
    while (!que.empty()) {
      P now = que.top();
      que.pop();
      int nowplace = now.second.first;
      int nowenem = now.first;
      int nowmoney = now.second.second;
      if (d[nowplace][nowmoney] < nowenem)
        continue;
      d[nowplace][nowmoney] = nowenem;
      for (int i = 0; i < (int)tab[nowplace].size(); i++) {
        edge e = tab[nowplace][i];
        if (e.cost <= nowmoney && d[e.to][nowmoney - e.cost] > nowenem) {
          d[e.to][nowmoney - e.cost] = nowenem;
          que.push(P(nowenem, p(e.to, nowmoney - e.cost)));
        }
        if (d[e.to][nowmoney] > nowenem + e.enem) {
          d[e.to][nowmoney] = nowenem + e.enem;
          que.push(P(nowenem + e.enem, p(e.to, nowmoney)));
        }
      }
    }
    int ans = MAX;
    for (int i = 0; i <= l; i++)
      ans = min(ans, d[n - 1][i]);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define MAX 100000000
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P;
// G@ê@à
struct edge {
  int to, cost, enem;
};
int main() {
  int n, m, l;
  while (cin >> n >> m >> l, n || m || l) {
    vector<edge> tab[110];
    for (int i = 0; i < m; i++) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--;
      b--;
      edge pre;
      pre.cost = d;
      pre.enem = e;
      pre.to = b;
      tab[a].push_back(pre);
      pre.to = a;
      tab[b].push_back(pre);
    }
    int d[110][110];
    // d[ê][à]=G;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 100; j++) {
        d[i][j] = MAX;
      }
    d[0][l] = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, p(0, l)));
    while (!que.empty()) {
      P now = que.top();
      que.pop();
      int nowplace = now.second.first;
      int nowenem = now.first;
      int nowmoney = now.second.second;
      if (d[nowplace][nowmoney] < nowenem)
        continue;
      d[nowplace][nowmoney] = nowenem;
      if (nowplace == n - 1)
        break;
      for (int i = 0; i < (int)tab[nowplace].size(); i++) {
        edge e = tab[nowplace][i];
        if (e.cost <= nowmoney && d[e.to][nowmoney - e.cost] > nowenem) {
          que.push(P(nowenem, p(e.to, nowmoney - e.cost)));
        }
        if (d[e.to][nowmoney] > nowenem + e.enem)
          que.push(P(nowenem + e.enem, p(e.to, nowmoney)));
      }
    }
    int ans = MAX;
    for (int i = 0; i <= l; i++)
      ans = min(ans, d[n - 1][i]);
    cout << ans << endl;
  }
  return 0;
}