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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int MAX_N = 101;
const int MAX_L = 101;

class C {
public:
  int p, dis, cost;
  C(int _p, int _dis, int _cost);
};

C::C(int _p, int _dis, int _cost) {
  p = _p;
  dis = _dis;
  cost = _cost;
}

int main() {
  int n, m, l;
  while (cin >> n >> m >> l && (n || m || l)) {
    vector<vector<C>> v(n, vector<C>());
    REP(i, m) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      a--;
      b--;
      v[a].push_back(C(b, c, d));
      v[b].push_back(C(a, c, d));
    }
    int closed[MAX_N][MAX_L];
    REP(i, MAX_N) {
      REP(j, MAX_L) { closed[i][j] = INF; }
    }
    queue<C> open;
    open.push(C(0, l, 0));

    while (!open.empty()) {
      C now = open.front();
      open.pop();

      if (now.cost >= closed[now.p][now.dis])
        continue;

      closed[now.p][now.dis] = now.cost;
      REP(i, v[now.p].size()) {
        C next = v[now.p][i];
        if (now.dis >= next.dis) {
          open.push(C(next.p, now.dis - next.dis, now.cost));
        }
        open.push(C(next.p, now.dis, now.cost + next.cost));
      }
    }

    int ans = INF;
    REP(i, MAX_L) { ans = min(ans, closed[n - 1][i]); }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int MAX_N = 110;

int n, m, l, ans;

class C {
public:
  int next, dis, thief;
  C(int _next, int _dis, int _thief);
};

C::C(int _next, int _dis, int _thief) {
  next = _next;
  dis = _dis;
  thief = _thief;
}

vector<vector<C>> v;

void dfs(int now, int money, int sum_thief, vector<bool> visited) {
  if (now == (n - 1)) {
    ans = min(ans, sum_thief);
  } else {
    int s = v[now].size();
    REP(i, s) {
      C next = v[now][i];
      if (visited[next.next] == false) {
        visited[next.next] = true;
        if (money >= next.dis) {
          dfs(next.next, money - next.dis, sum_thief, visited);
        }
        dfs(next.next, money, sum_thief + next.thief, visited);
        visited[next.next] = false;
      }
    }
  }
}

int main() {
  while (cin >> n >> m >> l && (n || m || l)) {
    ans = INF;
    v = vector<vector<C>>(n + 1, vector<C>());
    REP(i, m) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--;
      b--;
      v[a].push_back(C(b, d, e));
      v[b].push_back(C(a, d, e));
    }
    vector<bool> visited(n, false);
    dfs(0, l, 0, visited);
    cout << ans << endl;
  }
  return 0;
}