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

#include <bits/stdc++.h>
#define inf 1 << 29
using namespace std;

class State {
public:
  int n, bits;
  double time;
  State(int n, double time, int bits) : n(n), time(time), bits(bits) {}
  bool operator<(State s) const { return s.time < time; }
};

int main() {
  int n, m, p, a, b, x, y, z;
  double T[10], map[35][35];
  double dp[35][(1 << 9) + 1];
  while (1) {
    cin >> n >> m >> p >> a >> b;
    if (n == 0)
      break;
    a--;
    b--;
    for (int i = 0; i < n; i++)
      cin >> T[i];
    for (int i = 0; i < 31; i++) {
      for (int j = 0; j < 31; j++)
        map[i][j] = -1;
      for (int j = 0; j < (1 << 9) + 1; j++)
        dp[i][j] = inf;
    }
    for (int i = 0; i < p; i++) {
      cin >> x >> y >> z;
      x--;
      y--;
      map[x][y] = z;
      map[y][x] = z;
    }
    double ans = inf;
    queue<State> pq;
    pq.push(State(a, 0, 0));
    dp[a][0] = 0;
    while (!pq.empty()) {
      State u = pq.front();
      pq.pop();
      // cout<<u.time<<endl;
      if (dp[u.n][u.bits] < u.time)
        continue;

      if (u.n == b) {
        ans = min(ans, u.time);
        continue;
      }
      for (int i = 0; i < m; i++) {
        if (map[u.n][i] == -1)
          continue;
        for (int j = 0; j < n; j++) {
          if (u.bits & (1 << j))
            continue;
          double time = (u.time + map[u.n][i] / T[j]);
          int v = u.bits | (1 << j);
          if (dp[i][v] > time) {
            dp[i][v] = time;
            pq.push(State(i, time, v));
          }
        }
      }
    }
    if (ans >= inf)
      cout << "Impossible" << endl;
    else
      printf("%.10f\\n", ans);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define inf 1 << 29
using namespace std;

class State {
public:
  int n, bits;
  double time;
  State(int n, double time, int bits) : n(n), time(time), bits(bits) {}
  bool operator<(State s) const { return s.time < time; }
};

int main() {
  int n, m, p, a, b, x, y, z;
  double T[10], map[35][35];
  double dp[35][(1 << 8) + 1];
  while (1) {
    cin >> n >> m >> p >> a >> b;
    if (n == 0)
      break;
    a--;
    b--;
    for (int i = 0; i < n; i++)
      cin >> T[i];
    for (int i = 0; i < 31; i++) {
      for (int j = 0; j < 31; j++)
        map[i][j] = -1;
      for (int j = 0; j < (1 << 8) + 1; j++)
        dp[i][j] = inf;
    }
    for (int i = 0; i < p; i++) {
      cin >> x >> y >> z;
      x--;
      y--;
      map[x][y] = z;
      map[y][x] = z;
    }
    double ans = inf;
    priority_queue<State> pq;
    pq.push(State(a, 0, 0));
    dp[a][0] = 0;
    while (!pq.empty()) {
      State u = pq.top();
      pq.pop();
      // cout<<u.time<<endl;
      if (dp[u.n][u.bits] < u.time)
        continue;

      if (u.n == b) {
        ans = min(ans, u.time);
        continue;
      }
      for (int i = 0; i < n; i++) {
        if ((u.bits >> i) % 2 == 1)
          continue;
        for (int j = 0; j < m; j++) {
          if (map[u.n][j] == -1)
            continue;
          double time = (u.time + map[u.n][j] / T[i]);
          int v = u.bits | (1 << i);
          if (dp[j][v] < time)
            continue;
          dp[j][v] = time;
          pq.push(State(j, time, v));
        }
      }
    }
    if (ans >= inf)
      cout << "Impossible" << endl;
    else
      printf("%.10f\\n", ans);
  }
  return 0;
}