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

#include <bits/stdc++.h>
using namespace std;

// repetition
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

// const value
// const ll MOD = 1e9 + 7;
// const int dx[] = {0,1,0,-1};//{0,0,1,1,1,-1,-1,-1};
// const int dy[] = {1,0,-1,0};//{1,-1,0,1,-1,0,1,-1};

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
inline ll toLL(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

#define INF 1e15
struct edge {
  ll to;
  double cost;
};
typedef pair<ll, ll> P;
typedef pair<double, ll> PDL;
typedef pair<PDL, int> State;
struct graph {
  ll V;
  vector<vector<edge>> G;

  graph(ll n) { init(n); }

  void init(ll n) {
    V = n;
    G.resize(V);
  }

  void add_edge(ll s, ll t, double cost) {
    edge e;
    e.to = t, e.cost = cost;
    G[s].push_back(e);
  }

  double dijkstra(ll s, ll g, VI &t) {
    vector<vector<double>> dp(V, vector<double>(1 << t.size(), INF));
    dp[s][0] = 0.;
    priority_queue<State, vector<State>, greater<State>> que;
    que.push(State(PDL(0., 0), s));
    while (!que.empty()) {
      State p = que.top();
      que.pop();
      double current_dist = p.first.first;
      int current_node = p.second;
      ll current_thicket = p.first.second;

      if (dp[current_node][current_thicket] < current_dist)
        continue;
      for (auto e : G[current_node]) {
        rep(i, t.size()) {
          if (!!(current_thicket & (1 << i)))
            continue;
          if (current_dist + (e.cost / (double)t[i]) <
              (dp[e.to][current_thicket + (1 << i)])) {
            dp[e.to][current_thicket + (1 << i)] =
                current_dist + (e.cost / (double)t[i]);
            que.push(State(PDL(dp[e.to][current_thicket + (1 << i)],
                               current_thicket + (1 << i)),
                           e.to));
          }
        }
      }
    }
    double dist = INF;
    rep(i, (1 << t.size())) { dist = min(dist, dp[g][i]); }
    return dist;
  }
};

int main() {

  ll n, m, p, a, b;
  while (true) {
    cin >> n >> m >> p >> a >> b;
    if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0) {
      break;
    }
    a--, b--;
    VI t(n);
    rep(i, n) { cin >> t[i]; }
    graph g(m);
    rep(w, p) {
      ll x, y;
      double z;
      cin >> x >> y >> z;
      x--;
      y--;
      g.add_edge(x, y, z);
      g.add_edge(y, x, z);
    }

    double dist = g.dijkstra(a, b, t);

    if (dist == INF) {
      cout << "Impossible"
           << "\\n";
    } else {
      printf("%0.10lf\\n", dist);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const double inf = 1e9;
int goal;
double dfs(int cnt, int current, vector<bool> visited, deque<double> path,
           vector<double> &p, vector<vector<double>> &town) {

  visited[current] = true;
  if (current == goal) {
    sort(all(path), greater<int>());
    double res = 0.;
    rep(i, path.size()) { res += path[i] / p[i]; }
    return res;
  }
  if (cnt == 0)
    return inf;
  double res = inf;

  rep(i, town[current].size()) {
    if (visited[i])
      continue;
    if (town[current][i] != inf) {
      path.push_back(town[current][i]);
      res = min(res, dfs(cnt - 1, i, visited, path, p, town));
      path.pop_back();
    }
  }
  return res;
}
void solve(int n, int m, int p, int a) {
  vector<double> pa(n);
  rep(i, n) { scanf("%lf", &pa[i]); }
  sort(all(pa), greater<int>());
  vector<vector<double>> town(m, vector<double>(m, inf));
  rep(i, m) town[i][i] = 0.;
  rep(i, p) {
    int x, y;
    double cost;
    scanf("%d %d %lf", &x, &y, &cost);
    x--;
    y--;
    town[x][y] = cost;
    town[y][x] = cost;
  }
  vector<bool> visited(m, false);
  deque<double> path;

  double ans = dfs(n, a, visited, path, pa, town);
  if (ans == inf) {
    printf("Impossible\\n");
  } else {
    printf("%0.10lf\\n", ans);
  }
}
int main() {
  int n, m, p, a;
  while (true) {
    scanf("%d %d %d %d %d", &n, &m, &p, &a, &goal);
    if (!n && !m && !p && !a && !goal)
      break;
    goal--;
    solve(n, m, p, a - 1);
  }

  return 0;
}