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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

struct edge {
  int to, cost;
  edge(int to_, int cost_) {
    to = to_;
    cost = cost_;
  }
};
const int MAX_N = 101;
const int MAX_M = 1001;
const int INF = 1e+8;
vector<edge> G[MAX_N];
// memo[k][v] := テゥツ?づァツつケ v
// テ」ツ?セテ」ツ?ァテ」ツ?淌」ツ?ゥテ」ツつ甘ァツ敖?」ツ?湘」ツ?ョテ」ツ?ォ k
// テ・ツ崢榲」ツつィテ」ツδε」ツつクテ」ツ?ョテ」ツつウテ」ツつケテ」ツδ暗」ツつ?0
// テ」ツ?ォテ」ツ?療」ツ?淌ァツ環カテヲツ?凝」ツつ津ヲツ篠「テァツエツ「テ」ツ?療」ツ?淌」ツ?凝」ツ?ゥテ」ツ??」ツ??
bool memo[MAX_M][MAX_N];
int n, m, c;

struct State {
  // cost := テ」ツつウテ」ツつケテ」ツδ? e :=
  // テ」ツつィテ」ツδε」ツつクテ」ツ?ョテ」ツつウテ」ツつケテ」ツδ暗」ツつ津・ツ、ツ嘉ヲツ崢エテ」ツ?療」ツ?淌・ツ崢榲ヲツ閉ー,
  // v := テゥツ?づァツつケ
  int cost, e, v;
  State(int cost_, int e_, int v_) {
    cost = cost_;
    e = e_;
    v = v_;
  }
};
bool operator<(const State &a, const State &b) { return a.cost > b.cost; }

int solve() {
  for (int i = 0; i < MAX_M; i++)
    fill(memo[i], memo[i] + MAX_N, false);

  priority_queue<State> q;
  q.push(State(0, 0, 0));
  int res = INF;
  while (!q.empty()) {
    int cost = q.top().cost, e = q.top().e, v = q.top().v;
    q.pop();

    if (memo[e][v])
      continue;
    memo[e][v] = true;

    if (v == n - 1) { // テ」ツつエテ」ツδシテ」ツδォテ」ツ?ョテヲツ卍?
      res = min(res, e);
      continue;
    }

    for (int i = 0; i < G[v].size(); i++) {
      int to = G[v][i].to;
      int next_cost = cost + G[v][i].cost;

      if (!memo[e][to] &&
          next_cost <=
              c) { // テ」ツつィテ」ツδε」ツつクテ」ツ?ョテ」ツつウテ」ツつケテ」ツδ暗」ツつ津・ツ、ツ嘉ヲツ崢エテ」ツ?療」ツ?ェテ」ツ??
        q.push(State(next_cost, e, to));
      }
      next_cost -= G[v][i].cost;
      if (!memo[e + 1][to] &&
          e + 1 <=
              n) { // テ」ツつィテ」ツδε」ツつクテ」ツ?ョテ」ツつウテ」ツつケテ」ツδ暗」ツつ津・ツ、ツ嘉ヲツ崢エテ」ツ?凖」ツつ?
        q.push(State(next_cost, e + 1, to));
      }
    }
  }
  return res;
}

void add_edge(int u, int v, int cost) { G[u].push_back(edge(v, cost)); }

int main() {
  while (cin >> n >> m >> c, n || m || c) {
    for (int i = 0; i < MAX_N; i++)
      G[i].clear();

    for (int i = 0; i < m; i++) {
      int u, v, cost;
      cin >> u >> v >> cost;
      u--;
      v--;
      add_edge(u, v, cost);
    }
    cout << solve() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
void debug(const vector<int> &v) {
  cout << "{";
  for (int i = 0; i < v.size(); i++) {
    cout << (i ? ", " : "") << v[i];
  }
  cout << "}" << endl;
}

struct edge {
  int to, cost;
  edge(int to_, int cost_) {
    to = to_;
    cost = cost_;
  }
};
const int INF = 1e+8;
const int MAX_N = 101;
vector<edge> G[MAX_N];
vector<int> from[MAX_N];
map<P, int> es;
int d[MAX_N];
int n, m, c, ans;

void add_edge(int u, int v, int cost) {
  G[u].push_back(edge(v, cost));
  es[P(u, v)] = cost;
}

void dijkstra() {
  fill(d, d + MAX_N, INF);
  d[0] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, 0));

  while (!q.empty()) {
    int cost = q.top().first, v = q.top().second;
    q.pop();

    if (d[v] < cost)
      continue;

    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[v] + e.cost <= d[e.to]) {
        if (d[v] + e.cost < d[e.to]) {
          from[e.to].clear();
          from[e.to].push_back(v);
          d[e.to] = d[v] + e.cost;
          q.push(P(d[e.to], e.to));
        } else {
          from[e.to].push_back(v);
        }
      }
    }
  }
}

void get_path(int u, vector<int> v) {
  if (u == 0) {
    reverse(v.begin(), v.end());
    // debug(v);
    vector<int> vc;
    for (int i = 1; i < v.size(); i++) {
      vc.push_back(es[P(v[i - 1], v[i])]);
    }
    // debug( vc );
    sort(vc.rbegin(), vc.rend());
    int sum = 0;
    for (int i = 0; i < vc.size(); i++)
      sum += vc[i];
    for (int i = 0; i < vc.size(); i++) {
      if (sum - vc[i] <= c) {
        ans = min(ans, i + 1);
        break;
      }
      sum -= vc[i];
    }
  } else {
    for (int i = 0; i < from[u].size(); i++) {
      v.push_back(from[u][i]);
      get_path(from[u][i], v);
      v.pop_back();
    }
  }
}

void bfs() {
  fill(d, d + MAX_N, INF);
  d[0] = 0;
  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    int cost = d[v];

    if (v == n - 1)
      return;

    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[v] + 1 <= d[e.to]) {
        if (d[v] + 1 < d[e.to]) {
          from[e.to].clear();
          from[e.to].push_back(v);
          d[e.to] = d[v] + 1;
          q.push(e.to);
        } else {
          from[e.to].push_back(v);
        }
      }
    }
  }
}

int main() {
  while (cin >> n >> m >> c, n || m || c) {
    // initialize
    es.clear();
    for (int i = 0; i < MAX_N; i++) {
      G[i].clear();
      from[i].clear();
    }

    for (int i = 0; i < m; i++) {
      int u, v, cost;
      cin >> u >> v >> cost;
      u--;
      v--;
      add_edge(u, v, cost);
    }
    // bfs();
    ans = INF;
    vector<int> v;
    // v.push_back(n-1);
    // get_path(n-1, v);

    // for(int i=0 ; i < MAX_N ; i++ ) from[i].clear();
    // vector<int> v;
    // ans = INF;

    dijkstra();
    // v.clear();
    v.push_back(n - 1);
    get_path(n - 1, v);
    cout << ans << endl;
  }
}