Problem p00719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

typedef pair<double, pair<int, int>> type;

void solve(int n, int m, int p, int a, int b) {
  a--;
  b--;

  vector<int> t(m);
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }

  int dist[105][105];
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++)
      dist[i][j] = -1;
  }

  vector<int> x(p), y(p), z(p);
  for (int i = 0; i < p; i++) {
    cin >> x[i] >> y[i] >> z[i];
    x[i]--;
    y[i]--;
    dist[x[i]][y[i]] = dist[y[i]][x[i]] = z[i];
  }

  double dp[105][1 << 8];
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < (1 << 8); j++)
      dp[i][j] = 1e12;
  }
  dp[a][0] = 0;
  // cout<<1<<endl;
  // first -> コスト | second.first -> 現在地 second.second -> bit mask
  priority_queue<type, vector<type>, greater<type>> que;
  // cout<<1<<endl;
  que.push(mp(0, mp(a, 0)));
  // cout<<1<<endl;
  while (que.size()) {
    type top = que.top();
    que.pop();
    double cost = top.first;
    int now = top.second.first, now_mask = top.second.second;

    if (cost > dp[now][now_mask])
      continue;
    for (int i = 0; i < m; i++)
      if (dist[now][i] != -1) {
        for (int j = 0; j < n; j++) {
          int next_mask = now_mask;
          if (next_mask & (1 << j))
            continue;
          next_mask |= (1 << j);
          double next_cost = cost + double(dist[now][i]) / t[j];
          if (dp[i][next_mask] > next_cost) {
            dp[i][next_mask] = next_cost;
            que.push(mp(next_cost, mp(i, next_mask)));
          }
        }
      }
  }

  double ans = 1e12;
  for (int i = 0; i < (1 << 8); i++) {
    ans = min(ans, dp[b][i]);
  }
  if (ans == 1e12)
    printf("Impossible\n");
  else
    printf("%.8f\n", ans);
}

int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b) {
    if (!(n | m | p | a | b))
      break;
    solve(n, m, p, a, b);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

typedef pair<double, pair<int, int>> type;

void solve(int n, int m, int p, int a, int b) {
  a--;
  b--;

  vector<int> t(n);
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }

  int dist[105][105];
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 105; j++)
      dist[i][j] = -1;
  }

  vector<int> x(p), y(p), z(p);
  for (int i = 0; i < p; i++) {
    cin >> x[i] >> y[i] >> z[i];
    x[i]--;
    y[i]--;
    dist[x[i]][y[i]] = dist[y[i]][x[i]] = z[i];
  }

  double dp[105][1 << 8];
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < (1 << 8); j++)
      dp[i][j] = 1e12;
  }
  dp[a][0] = 0;
  // cout<<1<<endl;
  // first -> コスト | second.first -> 現在地 second.second -> bit mask
  priority_queue<type, vector<type>, greater<type>> que;
  // cout<<1<<endl;
  que.push(mp(0, mp(a, 0)));
  // cout<<1<<endl;
  while (que.size()) {
    type top = que.top();
    que.pop();
    double cost = top.first;
    int now = top.second.first, now_mask = top.second.second;

    if (cost > dp[now][now_mask])
      continue;
    for (int i = 0; i < m; i++)
      if (dist[now][i] != -1) {
        for (int j = 0; j < n; j++) {
          int next_mask = now_mask;
          if (next_mask & (1 << j))
            continue;
          next_mask |= (1 << j);
          double next_cost = cost + double(dist[now][i]) / t[j];
          if (dp[i][next_mask] > next_cost) {
            dp[i][next_mask] = next_cost;
            que.push(mp(next_cost, mp(i, next_mask)));
          }
        }
      }
  }

  double ans = 1e12;
  for (int i = 0; i < (1 << 8); i++) {
    ans = min(ans, dp[b][i]);
  }
  if (ans == 1e12)
    printf("Impossible\n");
  else
    printf("%.8f\n", ans);
}

int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b) {
    if (!(n | m | p | a | b))
      break;
    solve(n, m, p, a, b);
  }
  return 0;
}