Problem p00890 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

struct Edge {
  int s, t, w;
  Edge(int s, int t, int w) : s(s), t(t), w(w) {}
};
struct P {
  int s, num, w;
  P(int s, int num, int w) : s(s), num(num), w(w) {}
};
const bool operator<(const P &a, const P &b) { return a.w > b.w; }
int dist[100][100];

int main() {
  int n, m, c;
  while (cin >> n >> m >> c, n || m || c) {
    vector<Edge> g[n];
    REP(i, m) {
      int a, b, d;
      cin >> a >> b >> d;
      a--;
      b--;
      g[a].push_back(Edge(a, b, d));
    }
    REP(i, n) {
      REP(j, n) { dist[i][j] = INF; }
    }
    priority_queue<P> Q;
    Q.push(P(0, 0, 0));
    int ans = INF;
    while (!Q.empty()) {
      P e = Q.top();
      Q.pop();
      // cout << e.s << " " << e.num << " " << e.w << endl;
      if (e.s == n - 1 && e.w < c) {
        ans = min(ans, e.num);
      }
      if (dist[e.s][e.num] < e.w)
        continue;
      FOR(f, g[e.s]) {
        if (e.num + 1 < n && dist[f->t][e.num + 1] > e.w) {
          dist[f->t][e.num + 1] = e.w;
          Q.push(P(f->t, e.num + 1, e.w));
        }
        if (e.w + f->w <= c) {
          if (dist[f->t][e.num] > e.w + f->w) {
            dist[f->t][e.num] = e.w + f->w;
            Q.push(P(f->t, e.num, e.w + f->w));
          }
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

struct Edge {
  int s, t, w;
  Edge(int s, int t, int w) : s(s), t(t), w(w) {}
};
struct P {
  int s, num, w;
  P(int s, int num, int w) : s(s), num(num), w(w) {}
};
const bool operator<(const P &a, const P &b) { return a.w > b.w; }
int dist[100][100];

int main() {
  int n, m, c;
  while (cin >> n >> m >> c, n || m || c) {
    vector<Edge> g[n];
    REP(i, m) {
      int a, b, d;
      cin >> a >> b >> d;
      a--;
      b--;
      g[a].push_back(Edge(a, b, d));
    }
    REP(i, n) {
      REP(j, n) { dist[i][j] = INF; }
    }
    priority_queue<P> Q;
    Q.push(P(0, 0, 0));
    int ans = INF;
    while (!Q.empty()) {
      P e = Q.top();
      Q.pop();
      // cout << e.s << " " << e.num << " " << e.w << endl;
      if (e.s == n - 1 && e.w < c) {
        ans = min(ans, e.num);
      }
      if (dist[e.s][e.num] < e.w)
        continue;
      FOR(f, g[e.s]) {
        if (dist[f->t][e.num + 1] > e.w) {
          dist[f->t][e.num + 1] = e.w;
          Q.push(P(f->t, e.num + 1, e.w));
        }
        if (e.w + f->w <= c) {
          if (dist[f->t][e.num] > e.w + f->w) {
            dist[f->t][e.num] = e.w + f->w;
            Q.push(P(f->t, e.num, e.w + f->w));
          }
        }
      }
    }
    cout << ans << endl;
  }
}