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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second

using namespace std;

const int INF = 1e9;

int N, M, L;
pair<int, int> road[110][110]; //????????¢????????°???
int dp
    [110]
    [110]; // d[i][j]:?????????i,???????????????j?????¨????????????????????§?\\??????????????°??????°
/*
int dfs(int nowNode, int nowMoney, int nowPeople, set<int> state){
  if(nowNode == N-1) return nowPeople;
  if(dp[nowNode][nowMoney] != INF) return dp[nowNode][nowMoney];
  int ret = INF;
  state.insert(nowNode);
  REP(nextNode,0,N){
    if(road[nowNode][nextNode].F == INF || state.find(nextNode)!=state.end())
continue; if(road[nowNode][nextNode].F <= nowMoney){ ret = min(ret,
        min(dfs(nextNode,nowMoney,nowPeople + road[nowNode][nextNode].S, state),
            dfs(nextNode,nowMoney-road[nowNode][nextNode].F,nowPeople, state)));
    }else{
      ret = min(ret,dfs(nextNode,nowMoney,nowPeople + road[nowNode][nextNode].S,
state));
    }
  }
  return dp[nowNode][nowMoney] = ret;
}
*/

int main() {
  while (1) {
    cin >> N >> M >> L;
    if (N == 0 && M == L && L == 0)
      break;
    REP(i, 0, N) REP(j, 0, N) road[i][j] = make_pair(INF, INF);
    REP(i, 0, M) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      road[a - 1][b - 1] = make_pair(d, e);
      road[b - 1][a - 1] = make_pair(d, e);
    }

    REP(i, 0, N) REP(j, 0, L + 1) dp[i][j] = INF;

    /*
    set<int> s;
    cout<<dfs(0,L,0,s)<<endl;
    */

    priority_queue<pair<int, pair<int, int>>>
        PQ; //?????°???<????????????????????????>
    PQ.push(make_pair(0, make_pair(0, L)));
    dp[0][L] = 0;

    while (!PQ.empty()) {
      int nowNode = PQ.top().S.F;
      int nowMoney = PQ.top().S.S;
      int nowPeople = (-1) * PQ.top().F;
      PQ.pop();

      // test
      // cout<<"nowNode:"<<nowNode<<", nowMoney:"<<nowMoney<<",
      // nowPeople:"<<nowPeople<<endl;

      if (dp[nowNode][nowMoney] < nowPeople)
        continue;
      REP(nextNode, 0, N) {
        if (road[nowNode][nextNode].F == INF)
          continue;
        int nextMoney;
        int nextPeople;
        if (road[nowNode][nextNode].F <= nowMoney) {
          nextMoney = nowMoney - road[nowNode][nextNode].F;
          nextPeople = nowPeople;
          if (dp[nextNode][nextMoney] > nextPeople) {
            dp[nextNode][nextMoney] = nextPeople;
            PQ.push(make_pair((-1) * dp[nextNode][nextMoney],
                              make_pair(nextNode, nextMoney)));
            // test
            // cout<<"--- nextNode:"<<nextNode<<",
            // nextPeople:"<<nextPeople<<endl;
          }
        }
        nextMoney = nowMoney;
        nextPeople = nowPeople + road[nowNode][nextNode].S;
        if (dp[nextNode][nextMoney] > nextPeople) {
          dp[nextNode][nextMoney] = nextPeople;
          PQ.push(make_pair((-1) * dp[nextNode][nextMoney],
                            make_pair(nextNode, nextMoney)));
          // test
          // cout<<"--- nextNode:"<<nextNode<<", nextPeople:"<<nextPeople<<endl;
        }
      }
    }
    cout << *min_element(dp[N - 1], dp[N - 1] + L + 1) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second

using namespace std;

const int INF = 1e9;

int N, M, L;
pair<int, int> road[110][110]; //????????¢????????°???
int dp
    [110]
    [110]; // d[i][j]:?????????i,???????????????j?????¨????????????????????§?\\??????????????°??????°

int dfs(int nowNode, int nowMoney, int nowPeople, set<int> state) {
  if (nowNode == N - 1)
    return nowPeople;
  if (dp[nowNode][nowMoney] != INF)
    return dp[nowNode][nowMoney];
  int ret = INF;
  state.insert(nowNode);
  REP(nextNode, 0, N) {
    if (road[nowNode][nextNode].F == INF || state.find(nextNode) != state.end())
      continue;
    if (road[nowNode][nextNode].F <=
        nowMoney) { /*?????????????????????????????¢??¢???*/
      ret = min(ret, min(dfs(nextNode, nowMoney,
                             nowPeople + road[nowNode][nextNode].S, state),
                         dfs(nextNode, nowMoney - road[nowNode][nextNode].F,
                             nowPeople, state)));
    } else { /*?????????????????????????????¢??????*/
      ret = min(ret, dfs(nextNode, nowMoney,
                         nowPeople + road[nowNode][nextNode].S, state));
    }
  }
  return dp[nowNode][nowMoney] = ret;
}

int main() {
  while (1) {
    cin >> N >> M >> L;
    if (N == 0 && M == L && L == 0)
      break;
    REP(i, 0, N) REP(j, 0, N) road[i][j] = make_pair(INF, INF);
    REP(i, 0, M) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      road[a - 1][b - 1] = make_pair(d, e);
      road[b - 1][a - 1] = make_pair(d, e);
    }

    REP(i, 0, N) REP(j, 0, L + 1) dp[i][j] = INF;
    set<int> s;
    cout << dfs(0, L, 0, s) << endl;

    /*
    priority_queue< pair< int ,pair<int,int> > > PQ;
    //?????°???<????????????????????????> PQ.push(make_pair(0,make_pair(0,L)));
    dp[0][L] = 0; //?????????

    while(!PQ.empty()){
      int nowNode = PQ.top.S.F;
      int nowMoney = PQ.top.S.S;
      int nowPeople = (-1)*PQ.top.F;
      PQ.pop();
      if(dp[nowNode][nowMoney] < nowPeople) continue;
      REP(nextNode,0,N){
        if(road[nowNode][nextNode].F == INF) continue;
        REP(money,0,nowMoney){
          int nextMoney;
          int nextPeople;
          if(if(road[nowNode][nextNode].F <= nowMoney){
            nextMoney = nowMoney - road[nowNode][nextNode].F
            nextPeople = nowPeople;
          }else{
            nextMoney = nowMoney;
            nextPeople = nowPeople + road[nowNode][nextNode].S;
          }
          dp[nextNode][nextMoney] = min(dp[nextNode][nextMoney], nextPeople);
          PQ.push_back(make_pair((-1)*dp[nextNode][nextMoney],
    make_pair(nextNode,nextMoney) ));
        }
      }
    }
    cout<<*max_element(dp[N-1],dp[N-1]+L)
    */
  }

  return 0;
}