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

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define inf 100000007
using namespace std;

struct State {
  int speed, now, before;
  double time;
  State(double time, int speed, int now, int before)
      : time(time), speed(speed), now(now), before(before) {}
  bool operator<(State S) const { return S.time < time; }
};

int main() {
  int n, m, s, g, a, b, d, l;
  int dis[31][31], lim[31][31], S[3] = {-1, 0, 1};
  double dp[31][31][31], eps = 1e-8;

  while (1) {
    cin >> n >> m;
    if (n + m == 0)
      break;
    cin >> s >> g;
    s--;
    g--;
    for (int i = 0; i < 31; i++) {
      for (int j = 0; j < 31; j++) {
        dis[i][j] = inf;
        lim[i][j] = inf;
        for (int k = 0; k < 31; k++)
          dp[i][j][k] = inf;
      }
    }
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> d >> l;
      a--;
      b--;
      dis[a][b] = d;
      dis[b][a] = d;
      lim[a][b] = l;
      lim[b][a] = l;
    }

    double ans = inf;
    priority_queue<State> pq;
    pq.push(State(0, 0, s, s));
    dp[s][s][0] = 0;
    while (!pq.empty()) {
      State u = pq.top();
      pq.pop();
      //  cout<<u.now<<" "<<u.before<<endl;
      if (u.now == g && u.speed == 1) {
        ans = min(u.time, ans);
        continue;
      }
      if (u.time > dp[u.before][u.now][u.speed] + eps)
        continue;
      /*
      printf("%.10f",u.f.f);
      cout<<" "<<u.f.s<<" "<<u.s.f<<" "<<u.s.s<<endl;
      */
      for (int i = 0; i < n; i++) {
        if (i == u.now)
          continue;
        if (i == u.before)
          continue;
        if (dis[u.now][i] == inf)
          continue;
        for (int k = 0; k < 3; k++) {
          int speed = u.speed + S[k];
          if (speed < 1 || 30 < speed)
            continue;
          if (lim[u.now][i] < speed)
            continue;
          double time = (double)dis[u.now][i] / (double)speed;
          if (dp[u.now][i][speed] < time + u.time + eps)
            continue;
          dp[u.now][i][speed] = time + u.time;
          pq.push(State(time + u.time, speed, i, u.now));
        }
      }
    }
    if (ans >= inf - 1)
      cout << "unreachable" << endl;
    else
      printf("%.6f\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define inf 100000007
using namespace std;

struct State {

  int speed, now, before;
  double time;
  State(double time, int speed, int now, int before)
      : time(time), speed(speed), now(now), before(before) {}
  bool operator<(State S) const { return S.time < time; }
};

int main() {
  int n, m, s, g, a, b, d, l;
  int dis[30][30], lim[30][30], S[3] = {-1, 0, 1};
  double dp[30][30][30], eps = 1e-8;

  while (1) {
    cin >> n >> m;
    if (n + m == 0)
      break;
    cin >> s >> g;
    s--;
    g--;
    for (int i = 0; i < 30; i++) {
      for (int j = 0; j < 30; j++) {
        dis[i][j] = inf;
        lim[i][j] = inf;
        for (int k = 0; k < 30; k++)
          dp[i][j][k] = inf;
      }
    }
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> d >> l;
      a--;
      b--;
      dis[a][b] = d;
      dis[b][a] = d;
      lim[a][b] = l;
      lim[b][a] = l;
    }

    double ans = inf;
    priority_queue<State> pq;
    pq.push(State(0, 0, s, s));
    dp[s][s][1] = 0;
    while (!pq.empty()) {
      State u = pq.top();
      pq.pop();

      if (u.now == g && u.speed == 1) {
        ans = min(u.time, ans);
        continue;
      }
      if (u.time > dp[u.before][u.now][u.speed] + eps)
        continue;
      /*
      printf("%.10f",u.f.f);
      cout<<" "<<u.f.s<<" "<<u.s.f<<" "<<u.s.s<<endl;
      */
      for (int i = 0; i < n; i++) {
        if (i == u.now)
          continue;
        if (i == u.before)
          continue;
        if (dis[u.now][i] == inf)
          continue;
        for (int k = 0; k < 3; k++) {
          int speed = u.speed + S[k];
          if (speed < 1 || 30 < speed)
            continue;
          if (lim[u.now][i] < speed)
            continue;
          double time = (double)dis[u.now][i] / (double)speed;
          if (dp[u.now][i][speed] + eps < time + u.time)
            continue;
          dp[u.now][i][speed] = time + u.time;
          pq.push(State(time + u.time, speed, i, u.now));
        }
      }
    }
    if (ans >= inf - 1)
      cout << "unreachable" << endl;
    else
      printf("%.5f\\n", ans);
  }
  return 0;
}