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

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

struct state {
  int city;
  double cost;
  unsigned ticket;
  state(int c, double d, unsigned t) : city(c), cost(d), ticket(t) {}
  bool operator<(const state &that) const { return cost > that.cost; }
};

int main() {
  int N, M, P, A, B;
  while (scanf("%d%d%d%d%d", &N, &M, &P, &A, &B) != EOF && N != 0) {
    --A;
    --B;
    static int tickets[8];
    for (int i = 0; i < N; i++) {
      scanf("%d", &tickets[i]);
    }
    vector<pair<int, int>> g[30];
    for (int i = 0; i < P; i++) {
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      --x;
      --y;
      g[x].push_back(make_pair(y, z));
      g[y].push_back(make_pair(x, z));
    }

    static double dists[30][1 << 8];
    for (int i = 0; i < M; i++) {
      fill_n(dists[i], 1 << N, 1e10);
    }
    dists[A][(1 << N) - 1] = 0;
    priority_queue<state> q;
    q.push(state(A, 0.0, (1 << N) - 1));
    while (!q.empty()) {
      const int n = q.top().city;
      const double cost = q.top().cost;
      const unsigned ticket = q.top().ticket;
      q.pop();
      if (n == B) {
        printf("%.3f\\n", cost);
        goto NEXT;
      }

      for (vector<pair<int, int>>::const_iterator it(g[n].begin());
           it != g[n].end(); ++it) {
        const int to = it->first;
        const int d = it->second;
        for (int i = 0; i < N; i++) {
          if (ticket & (1 << i)) {
            const unsigned s = ticket & ~(1 << i);
            const double c = cost + static_cast<double>(d) / tickets[i];
            if (c < dists[to][s]) {
              dists[to][s] = c;
              q.push(state(to, c, s));
            }
          }
        }
      }
    }
    puts("Impossible");
  NEXT:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

typedef std::tuple<int, int, double> T;

std::vector<T> dis_city;
std::vector<int> num_basya;
int n, m, p, a, b;

std::vector<std::tuple<int, int>> table;
double cost[1 << 8][30];

double search() {
  for (int i = 0; i < (1 << n); ++i) {
    for (int k = 0; k < m; ++k) {
      cost[i][k] = 0xffff;
    }
  }
  cost[(1 << n) - 1][a] = 0;
  double ans = 0xffff;

  std::priority_queue<std::tuple<double, int, int>> q;
  q.push(std::make_tuple(0, a, (1 << n) - 1));
  while (!q.empty()) {
    double c = std::get<0>(q.top());
    int from = std::get<1>(q.top());
    int s = std::get<2>(q.top());
    q.pop();

    if (from == b) {
      ans = c;
    }
    for (int i = 0; i < n; ++i) {
      if (s & (1 << i)) {
        for (int j = 0; j < dis_city.size(); ++j) {
          if (std::get<0>(dis_city[j]) == from) {
            int to = std::get<1>(dis_city[j]);
            double f = c + std::get<2>(dis_city[j]) / num_basya[i];
            if (cost[s ^ (1 << i)][to] > f) {
              cost[s ^ (1 << i)][to] = f;
              q.push(std::make_tuple(f, to, s ^ (1 << i)));
            }
          }
        }
      }
    }
  }
  return ans;
}

int main(void) {

  while (true) {
    dis_city.clear();
    num_basya.clear();
    std::cin >> n >> m >> p >> a >> b;
    if (n == 0 && m == 0)
      return 0;
    a--;
    b--;

    num_basya.resize(n);
    for (auto &v : num_basya) {
      std::cin >> v;
    }
    std::sort(num_basya.begin(), num_basya.end());

    for (int i = 0; i < p; ++i) {
      int x, y, z;
      std::cin >> x;
      std::cin >> y;
      std::cin >> z;
      x--;
      y--;

      T t1(x, y, z), t2(y, x, z);
      dis_city.push_back(t1);
      dis_city.push_back(t2);
    }

    double ans = search();
    if (ans > 65530) {
      std::cout << "Impossible" << std::endl;
    } else {
      printf("%.4f\\n", ans);
    }
  }

  return 0;
}