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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define fs first
#define sc second
#define pb push_back
#define mk make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> p) {
  return out << "(" << p.fs << ", " << p.sc << ")";
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "{";
  rep(i, v.size()) out << v[i] << ", ";
  return out << "}" << endl;
}

#define N 30
double d[N + 1][N][N];
// -time, from, current, speed
struct elem {
  double t;
  int f, c, s;
  elem(){};
  elem(double time, int from, int cur, int sp) {
    t = time;
    f = from;
    c = cur;
    s = sp;
  }
  bool operator<(const elem r) const {
    return mk(mk(t, f), mk(c, s)) < mk(mk(r.t, r.f), mk(r.c, r.s));
  }
};
ostream &operator<<(ostream &out, const elem &e) {
  return out << "{" << e.t << ", " << e.f << ", " << e.c << ", " << e.s << "}";
}

#define INF (1e12)
int main() {
  int n, m, s, g;
  while (cin >> n >> m, n) {
    cin >> s >> g;
    s--;
    g--;
    fill(d[0][0], d[0][0] + (N + 1) * N * N, INF);
    vector<pair<int, double>>
        e[N + 2][N]; // e[c][x] : from x at vert c : (to, time) list
    rep(i, m) {
      int x, y, di, c;
      cin >> x >> y >> di >> c;
      x--;
      y--;
      reps(j, 1, c + 1) {
        double time = di / (double)j;
        e[j][x].pb(mk(y, time));
        e[j][y].pb(mk(x, time));
      }
    }
    priority_queue<elem> q;
    q.push(elem(-0.0, -1, s, 0)); // initial state
    while (!q.empty()) {
      elem el = q.top();
      double dst = -el.t;
      int from = el.f, cur = el.c, sp = el.s;
      q.pop();
      if (from >= 0) {
        if (d[sp][cur][from] < INF)
          continue;
        //				if(d[sp][cur][from] < dst) continue;
        d[sp][cur][from] = dst;
      }
      //			cout << el << endl;
      //			if(sp == 1 && cur == g) break;
      rep(i, 3) {
        int ci = sp - 1 + i;
        if (ci < 1 || ci > N)
          continue;
        rep(j, e[ci][cur].size()) {
          pair<int, double> p = e[ci][cur][j];
          int to = p.fs;
          if (from == to)
            continue;
          double tmp = dst + p.sc;
          q.push(elem(-tmp, cur, to, ci));
        }
      }
    }
    /*		reps(i, 1, N+1){
                            cout << "v : " << i << endl;
                            rep(j, n) cout << d[i][j] << ", ";
                            cout << endl;
                    }
    */
    double ans = INF;
    rep(i, n) ans = min(ans, d[1][g][i]);
    if (ans >= INF)
      cout << "unreachable" << endl;
    else
      cout << fixed << setprecision(10) << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define fs first
#define sc second
#define pb push_back
#define mk make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> p) {
  return out << "(" << p.fs << ", " << p.sc << ")";
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "{";
  rep(i, v.size()) out << v[i] << ", ";
  return out << "}" << endl;
}

#define N 30
double d[N + 1][N][N];
// -time, from, current, speed
struct elem {
  double t;
  int f, c, s;
  elem(){};
  elem(double time, int from, int cur, int sp) {
    t = time;
    f = from;
    c = cur;
    s = sp;
  }
  bool operator<(const elem r) const {
    return mk(mk(t, f), mk(c, s)) < mk(mk(r.t, r.f), mk(r.c, r.s));
  }
};
ostream &operator<<(ostream &out, const elem &e) {
  return out << "{" << e.t << ", " << e.f << ", " << e.c << ", " << e.s << "}";
}

#define INF (1e12)
int main() {
  int n, m, s, g;
  while (cin >> n >> m, n) {
    cin >> s >> g;
    s--;
    g--;
    fill(d[0][0], d[0][0] + (N + 1) * N * N, -1);
    vector<pair<int, double>>
        e[N + 2][N]; // e[c][x] : from x at vert c : (to, time) list
    rep(i, m) {
      int x, y, di, c;
      cin >> x >> y >> di >> c;
      x--;
      y--;
      reps(j, 1, c + 1) {
        double time = di / (double)j;
        e[j][x].pb(mk(y, time));
        e[j][y].pb(mk(x, time));
      }
    }
    priority_queue<elem> q;
    q.push(elem(-0.0, -1, s, 0)); // initial state
    while (!q.empty()) {
      elem el = q.top();
      double dst = -el.t;
      int from = el.f, cur = el.c, sp = el.s;
      q.pop();
      if (d[sp][cur][from] >= 0 && d[sp][cur][from] < dst) {
        continue;
      }
      d[sp][cur][from] = dst;
      //			cout << el << endl;
      //			if(sp == 1 && cur == g) break;
      rep(i, 3) {
        int ci = sp - 1 + i;
        if (ci < 1 || ci > N + 1)
          continue;
        rep(j, e[ci][cur].size()) {
          pair<int, double> p = e[ci][cur][j];
          int to = p.fs;
          if (from == to)
            continue;
          double tmp = dst + p.sc;
          q.push(elem(-tmp, cur, to, ci));
        }
      }
    }
    /*		reps(i, 1, N+1){
                            cout << "v : " << i << endl;
                            rep(j, n) cout << d[i][j] << ", ";
                            cout << endl;
                    }
    */
    double ans = INF;
    rep(i, n) if (d[1][g][i] >= 0) ans = min(ans, d[1][g][i]);
    if (ans >= INF)
      cout << "unreachable" << endl;
    else
      cout << fixed << setprecision(10) << ans << endl;
  }
  return 0;
}