Problem p00719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
  while (true) {
    int n, m, p, a, b;
    cin >> n >> m >> p >> a >> b;
    if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0)
      break;
    --a;
    --b;
    vector<int> card(n);
    REP(i, n) cin >> card[i];
    vector<vector<P>> l(m);
    REP(i, p) {
      int x, y, z;
      cin >> x >> y >> z;
      --x;
      --y;
      l[x].pb(P(y, z));
      l[y].pb(P(x, z));
    }
    vector<vector<double>> d(m, vector<double>(pow(2, n), INF * INF));
    d[a][pow(2, n) - 1] = 0;
    priority_queue<pair<pair<double, ll>, ll>,
                   vector<pair<pair<double, ll>, ll>>,
                   greater<pair<pair<double, ll>, ll>>>
        que;
    que.push(pair<pair<double, ll>, ll>(pair<double, ll>(0, a), pow(2, n) - 1));
    while (!que.empty()) {
      pair<pair<double, ll>, ll> np = que.top();
      double o = np.first.first;
      ll p = np.first.second;
      que.pop();
      if (d[p][np.second] != o)
        continue;
      REP(i, l[p].size()) {
        REP(j, n) {
          if ((np.second & (int)pow(2, j)) != 0) {
            if (d[l[p][i].first][(np.second ^ (int)pow(2, j))] >
                d[p][np.second] + (double)l[p][i].second / card[j]) {
              d[l[p][i].first][(np.second ^ (int)pow(2, j))] =
                  d[p][np.second] + (double)l[p][i].second / card[j];
              que.push(pair<pair<double, ll>, ll>(
                  pair<double, ll>(
                      d[l[p][i].first][(np.second ^ (int)pow(2, j))],
                      l[p][i].first),
                  (np.second ^ (int)pow(2, j))));
            }
          }
        }
      }
    }
    double res = INF;
    REP(i, pow(2, n)) { res = min(res, d[b][i]); }
    if (res != INF)
      cout << fixed << showpoint << setprecision(39) << res << endl;
    else
      cout << "Impossible" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
  while (true) {
    int n, m, p, a, b;
    cin >> n >> m >> p >> a >> b;
    if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0)
      break;
    --a;
    --b;
    vector<int> card(n);
    REP(i, n) cin >> card[i];
    vector<vector<P>> l(m);
    REP(i, p) {
      int x, y, z;
      cin >> x >> y >> z;
      --x;
      --y;
      l[x].pb(P(y, z));
      l[y].pb(P(x, z));
    }
    vector<vector<double>> d(m, vector<double>(pow(2, n), INF * INF));
    d[a][pow(2, n) - 1] = 0;
    priority_queue<pair<pair<double, ll>, ll>> que;
    que.push(pair<pair<double, ll>, ll>(pair<double, ll>(0, a), pow(2, n) - 1));
    while (!que.empty()) {
      pair<pair<double, ll>, ll> np = que.top();
      double o = np.first.first;
      ll p = np.first.second;
      que.pop();
      if (d[p][np.second] != o)
        continue;
      REP(i, l[p].size()) {
        REP(j, n) {
          if ((np.second & (int)pow(2, j)) != 0) {
            if (d[l[p][i].first][(np.second ^ (int)pow(2, j))] >
                d[p][np.second] + (double)l[p][i].second / card[j]) {
              d[l[p][i].first][(np.second ^ (int)pow(2, j))] =
                  d[p][np.second] + (double)l[p][i].second / card[j];
              que.push(pair<pair<double, ll>, ll>(
                  pair<double, ll>(
                      d[l[p][i].first][(np.second ^ (int)pow(2, j))],
                      l[p][i].first),
                  (np.second ^ (int)pow(2, j))));
            }
          }
        }
      }
    }
    double res = INF;
    REP(i, pow(2, n)) { res = min(res, d[b][i]); }
    if (res != INF)
      cout << res << endl;
    else
      cout << "impossible" << endl;
  }
}