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

#include <iostream>
#include <queue>

using namespace std;

struct Edge {
  int to, dist;
  Edge() {}
  Edge(int t, int d) : to(t), dist(d) {}
};
typedef vector<vector<Edge>> Graph;

int main() {
  for (int n, m, p, a, b; cin >> n >> m >> p >> a >> b, n | m | p | a | b;) {
    vector<int> ts(n);
    for (int i = 0; i < n; i++)
      cin >> ts[i];
    Graph g(m);
    for (int i = 0; i < p; i++) {
      int from, to, dist;
      cin >> from >> to >> dist;
      g[from - 1].push_back(Edge(to - 1, dist));
      g[to - 1].push_back(Edge(from - 1, dist));
    }

    vector<vector<double>> times(m, vector<double>(1 << n, 1 << 29));
    vector<vector<int>> flgs(m, vector<int>(1 << n));
    times[a - 1][0] = 0;
    for (;;) {
      int cv = -1, cb = -1;
      for (int i = 0; i < m; i++)
        for (int j = 0; j < (1 << n); j++)
          if (!flgs[i][j] && (cv == -1 || times[i][j] < times[cv][cb]))
            cv = i, cb = j;
      if (cv == -1)
        break;
      flgs[cv][cb] = 1;

      for (int i = 0; i < g[cv].size(); i++) {
        Edge e = g[cv][i];
        for (int j = 0; j < n; j++) {
          double t = times[cv][cb] + (double)e.dist / ts[j];
          if (!(cb & 1 << j) && times[e.to][cb | 1 << j] > t)
            times[e.to][cb | 1 << j] = t;
        }
      }
    }

    double res = 1 << 29;
    for (int i = 0; i < times[b - 1].size(); i++)
      res = min(res, times[b - 1][i]);
    if (res == (1 << 29))
      cout << "Impossible" << endl;
    else {
      cout.precision(3);
      cout << fixed << res << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

struct Edge {
  int to, weight;
  Edge() {}
  Edge(int t, int w) : to(t), weight(w) {}
};
typedef vector<vector<Edge>> Graph;

struct State {
  double dist;
  int last, bit;
  State() {}
  State(double d, int l, int b) : dist(d), last(l), bit(b) {}
  bool operator<(const State &s) const { return dist > s.dist; } // inverse!
};

int main() {
  for (int n, m, p, a, b; cin >> n >> m >> p >> a >> b, n | m | p | a | b;) {
    vector<int> ts(n);
    for (int i = 0; i < n; i++)
      cin >> ts[i];
    Graph g(m);
    for (int i = 0; i < p; i++) {
      int from, to, weight;
      cin >> from >> to >> weight;
      g[from - 1].push_back(Edge(to - 1, weight));
      g[to - 1].push_back(Edge(from - 1, weight));
    }

    vector<vector<double>> dist(m, vector<double>(1 << n, 1 << 29));
    priority_queue<State> pq;
    pq.push(State(0, a - 1, 0));
    while (pq.size()) {
      State cur = pq.top();
      pq.pop();
      if (cur.dist > dist[cur.last][cur.bit])
        continue;
      dist[cur.last][cur.bit] = cur.dist;
      for (int i = 0; i < g[cur.last].size(); i++) {
        Edge e = g[cur.last][i];
        for (int j = 0; j < n; j++) {
          if (!(cur.bit & 1 << j)) {
            pq.push(State(cur.dist + (double)e.weight / ts[j], e.to,
                          cur.bit | 1 << j));
          }
        }
      }
    }

    double res = 1 << 29;
    for (int i = 0; i < dist[b - 1].size(); i++)
      res = min(res, dist[b - 1][i]);
    if (res == (1 << 29))
      cout << "Impossible" << endl;
    else {
      cout.precision(6);
      cout << fixed << res << endl;
    }
  }
  return 0;
}