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

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct Edge {
  int from, to, dis;
  Edge(int f, int t, int d) {
    from = f;
    to = t;
    dis = d;
  }
};

struct Node {
  int num, state;
  double cost;
  Node(int n, double c, int st) {
    num = n;
    cost = c;
    state = st;
  }
};

int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    vector<int> t(n);
    vector<vector<Edge>> G(m + 1);
    for (int i = 0; i < n; ++i)
      cin >> t[i];
    for (int i = 0; i < p; ++i) {
      int x, y, z;
      cin >> x >> y >> z;
      G[x].emplace_back(x, y, z);
      G[y].emplace_back(y, x, z);
    }
    // dijkstra
    auto compare = [](const Node n1, const Node n2) {
      return n1.cost > n2.cost;
    };
    priority_queue<Node, vector<Node>, decltype(compare)> que(compare);
    que.push(Node(a, 0.0, (1 << n) - 1));
    double ans = -1;
    vector<vector<int>> visited(m + 1, vector<int>(1 << n, 0));
    while (!que.empty()) {
      Node now = que.top();
      que.pop();
      int num = now.num, state = now.state;
      if (now.num == b) {
        ans = now.cost;
        break;
      }
      if (visited[num][state])
        continue;
      visited[num][state] = 1;
      for (int i = 0; i < state; ++i) {
        if ((i & state) == state)
          visited[num][i] = 1;
      }
      int sup = G[now.num].size();
      for (int i = 0; i < sup; ++i) {
        Edge e = G[num][i];
        for (int j = 0; j < n; ++j) {
          if (!((state >> j) % 2))
            continue;
          if (visited[e.to][state - (1 << j)])
            continue;
          que.push(Node(e.to, now.cost + (double)e.dis / (double)t[j],
                        state - (1 << j)));
        }
      }
    }
    if (ans == -1)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct Edge {
  int from, to, dis;
  Edge(int f, int t, int d) {
    from = f;
    to = t;
    dis = d;
  }
};

struct Node {
  int num, state;
  double cost;
  Node(int n, double c, int st) {
    num = n;
    cost = c;
    state = st;
  }
};

int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    vector<int> t(n);
    vector<vector<Edge>> G(m + 1);
    for (int i = 0; i < n; ++i)
      cin >> t[i];
    for (int i = 0; i < p; ++i) {
      int x, y, z;
      cin >> x >> y >> z;
      G[x].emplace_back(x, y, z);
      G[y].emplace_back(y, x, z);
    }
    // dijkstra
    auto compare = [](const Node n1, const Node n2) {
      return n1.cost > n2.cost;
    };
    priority_queue<Node, vector<Node>, decltype(compare)> que(compare);
    que.push(Node(a, 0.0, (1 << n) - 1));
    double ans = -1;
    vector<vector<int>> visited(m + 1, vector<int>(1 << n, 0));
    visited[a][(1 << n) - 1] = 0;
    while (!que.empty()) {
      Node now = que.top();
      que.pop();
      int num = now.num, state = now.state;
      if (now.num == b) {
        ans = now.cost;
        break;
      }
      visited[num][state] = 1;
      for (int i = 0; i < state; ++i) {
        if ((i & state) == state)
          visited[num][i] = 1;
      }
      int sup = G[now.num].size();
      for (int i = 0; i < sup; ++i) {
        Edge e = G[num][i];
        for (int j = 0; j < n; ++j) {
          if (!((state >> j) % 2))
            continue;
          if (visited[e.to][state - (1 << j)])
            continue;
          que.push(Node(e.to, now.cost + (double)e.dis / (double)t[j],
                        state - (1 << j)));
        }
      }
    }
    if (ans == -1)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
}