Problem p00890 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define RFOR(it, c)                                                            \
  for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define exist(c, v) (find((c).begin(), (c).end(), (v)) != (c).end())

template <class T> inline void chmax(T &a, const T &b) {
  if (b > a)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (b < a)
    a = b;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
  return os;
}

struct state {
  int v, cnt, d;
  state(int v, int c, int d) : v(v), cnt(c), d(d) {}
  bool operator>(const state &s) const { return d > s.d; }
};

struct edge {
  int to, cost;
  edge(int t, int c) : to(t), cost(c) {}
};

int ans;
vector<vector<edge>> es;
vector<int> dist;

void dijkstra(int s, int t, int limit) {
  ans = INT_MAX;
  priority_queue<state, vector<state>, greater<state>> que;
  vector<vector<int>> dist(es.size(), vector<int>(es.size(), INT_MAX));
  que.push(state(s, 0, 0));
  dist[s][0] = 0;

  while (!que.empty()) {
    const state s = que.top();
    que.pop();
    if (dist[s.v][s.cnt] < s.d || s.cnt >= (int)es.size())
      continue;

    if (s.d > limit)
      return;

    if (s.v == t)
      chmin(ans, s.cnt);

    for (int i = 0; i < es[s.v].size(); ++i) {
      const edge &e = es[s.v][i];
      if (dist[e.to][s.cnt + 1] > s.d) {
        dist[e.to][s.cnt + 1] = s.d;
        que.push(state(e.to, s.cnt + 1, s.d));
      }

      const int next_dist = dist[s.v][s.cnt] + e.cost;

      if (dist[e.to][s.cnt] > next_dist) {
        dist[e.to][s.cnt] = next_dist;
        que.push(state(e.to, s.cnt, next_dist));
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  for (int n, m, c; cin >> n >> m >> c, n;) {
    es.clear();
    es.resize(n);
    for (int i = 0; i < m; ++i) {
      int f, t, d;
      cin >> f >> t >> d;
      --f;
      --t;
      es[f].push_back(edge(t, d));
    }

    dijkstra(0, n - 1, c);
    cout << ans << endl;
  }

  return EXIT_SUCCESS;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define RFOR(it, c)                                                            \
  for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define exist(c, v) (find((c).begin(), (c).end(), (v)) != (c).end())

template <class T> inline void chmax(T &a, const T &b) {
  if (b > a)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (b < a)
    a = b;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
  return os;
}

struct state {
  int v, cnt, d;
  state(int v, int c, int d) : v(v), cnt(c), d(d) {}
  bool operator>(const state &s) const { return d > s.d; }
};

struct edge {
  int to, cost;
  edge(int t, int c) : to(t), cost(c) {}
};

int ans;
vector<vector<edge>> es;
vector<int> dist;

void dijkstra(int s, int t, int limit) {
  ans = INT_MAX;
  priority_queue<state, vector<state>, greater<state>> que;
  vector<vector<int>> dist(es.size(), vector<int>(es.size() + 1, INT_MAX));
  que.push(state(s, 0, 0));
  dist[s][0] = 0;

  while (!que.empty()) {
    const state s = que.top();
    que.pop();
    if (dist[s.v][s.cnt] < s.d || s.cnt >= (int)es.size())
      continue;

    if (s.d > limit)
      return;

    if (s.v == t)
      chmin(ans, s.cnt);

    for (int i = 0; i < es[s.v].size(); ++i) {
      const edge &e = es[s.v][i];
      if (dist[e.to][s.cnt + 1] > s.d) {
        dist[e.to][s.cnt + 1] = s.d;
        que.push(state(e.to, s.cnt + 1, s.d));
      }

      const int next_dist = dist[s.v][s.cnt] + e.cost;

      if (dist[e.to][s.cnt] > next_dist) {
        dist[e.to][s.cnt] = next_dist;
        que.push(state(e.to, s.cnt, next_dist));
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  for (int n, m, c; cin >> n >> m >> c, n;) {
    es.clear();
    es.resize(n);
    for (int i = 0; i < m; ++i) {
      int f, t, d;
      cin >> f >> t >> d;
      --f;
      --t;
      es[f].push_back(edge(t, d));
    }

    dijkstra(0, n - 1, c);
    cout << ans << endl;
  }

  return EXIT_SUCCESS;
}