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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

double dp[30][1 << 8];

struct edge {
  int to, cost;
};
typedef vector<edge> ve;

int main(void) {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    a--, b--;

    vi t(n);
    rep(i, n) cin >> t[i];

    vector<ve> v(m);

    rep(i, p) {
      int x, y, z;
      cin >> x >> y >> z;
      x--;
      y--;
      struct edge e;
      e.to = y;
      e.cost = z;
      v[x].push_back(e);
      e.to = x;
      v[y].push_back(e);
    }

    rep(i, m) rep(j, 1 << n) dp[i][j] = INF;
    dp[a][0] = 0;

    queue<pii> q;
    q.push(pii(a, 0)); // 現在地、使った馬車券

    while (q.size()) {
      int now = q.front().first;
      int s = q.front().second;
      q.pop();
      // cout << now << " " << s << endl;
      rep(i, v[now].size()) {
        int next = v[now][i].to;
        int c = v[now][i].cost;

        rep(j, n) if ((s & (1 << j)) == 0) { // 馬車券 t[j] を使える
          double x = 1.0 * c / t[j];         // かかる時間
          int ns = s | (1 << j);             // 行き先
          if (dp[next][ns] > dp[now][s] + x) {
            dp[next][ns] = dp[now][s] + x;
            q.push(pii(next, ns));
          }
        }
      }
    }

    double ans = INF;
    rep(j, 1 << n) ans = min(ans, dp[b][j]);

    if (abs(ans - INF) < EPS) {
      cout << "Impossible" << endl;
    } else {
      cout << shosu(5) << ans << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int main(void) {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    a--, b--;
    vi t(n);
    rep(i, n) cin >> t[i];
    sort(all(t));
    int cost[30][30] = {};
    rep(i, p) {
      int x, y, z;
      cin >> x >> y >> z;
      x--;
      y--;
      cost[x][y] = z;
      cost[y][x] = z;
    }
    double ans = INF;
    bool c = false;

    if (cost[a][b]) {
      ans = 1.0 * cost[a][b] / t[n - 1];
      c = true;
    }

    if (n >= 2) {
      rep(v1, 30) {
        vi v;
        v.push_back(cost[a][v1]);
        v.push_back(cost[v1][b]);
        sort(all(v));
        if (v[0] <= 0 || n < v.size())
          continue;
        double tmp = 0;
        rep(i, v.size()) tmp += 1.0 * v[i] / t[n - v.size() + i];
        ans = min(ans, tmp);
        c = true;
      }
    }

    if (n >= 3) {
      rep(v1, 30) loop(v2, v1 + 1, 30) {
        vi v;
        v.push_back(cost[a][v1]);
        v.push_back(cost[v1][v2]);
        v.push_back(cost[v2][b]);
        sort(all(v));
        if (v[0] <= 0 || n < v.size())
          continue;
        double tmp = 0;
        rep(i, v.size()) tmp += 1.0 * v[i] / t[n - v.size() + i];
        ans = min(ans, tmp);
        c = true;
      }
    }

    if (n >= 4) {
      rep(v1, 30) loop(v2, v1 + 1, 30) loop(v3, v2 + 1, 30) {
        vi v;
        v.push_back(cost[a][v1]);
        v.push_back(cost[v1][v2]);
        v.push_back(cost[v2][v3]);
        v.push_back(cost[v3][b]);
        sort(all(v));
        if (v[0] <= 0 || n < v.size())
          continue;
        double tmp = 0;
        rep(i, v.size()) tmp += 1.0 * v[i] / t[n - v.size() + i];
        ans = min(ans, tmp);
        c = true;
      }
    }

    if (n >= 5) {
      rep(v1, 30) loop(v2, v1 + 1, 30) loop(v3, v2 + 1, 30)
          loop(v4, v3 + 1, 30) {
        vi v;
        v.push_back(cost[a][v1]);
        v.push_back(cost[v1][v2]);
        v.push_back(cost[v2][v3]);
        v.push_back(cost[v3][v4]);
        v.push_back(cost[v4][b]);
        sort(all(v));
        if (v[0] <= 0 || n < v.size())
          continue;
        double tmp = 0;
        rep(i, v.size()) tmp += 1.0 * v[i] / t[n - v.size() + i];
        ans = min(ans, tmp);
        c = true;
      }
    }

    if (n >= 6) {
      rep(v1, 30) loop(v2, v1 + 1, 30) loop(v3, v2 + 1, 30) loop(v4, v3 + 1, 30)
          loop(v5, v4 + 1, 30) {
        vi v;
        v.push_back(cost[a][v1]);
        v.push_back(cost[v1][v2]);
        v.push_back(cost[v2][v3]);
        v.push_back(cost[v3][v4]);
        v.push_back(cost[v4][v5]);
        v.push_back(cost[v5][b]);
        sort(all(v));
        if (v[0] <= 0 || n < v.size())
          continue;
        double tmp = 0;
        rep(i, v.size()) tmp += 1.0 * v[i] / t[n - v.size() + i];
        ans = min(ans, tmp);
        c = true;
      }
    }

    if (n >= 7) {
      rep(v1, 30) loop(v2, v1 + 1, 30) loop(v3, v2 + 1, 30) loop(v4, v3 + 1, 30)
          loop(v5, v4 + 1, 30) loop(v6, v5 + 1, 30) {
        vi v;
        v.push_back(cost[a][v1]);
        v.push_back(cost[v1][v2]);
        v.push_back(cost[v2][v3]);
        v.push_back(cost[v3][v4]);
        v.push_back(cost[v4][v5]);
        v.push_back(cost[v5][v6]);
        v.push_back(cost[v6][b]);
        sort(all(v));
        if (v[0] <= 0 || n < v.size())
          continue;
        double tmp = 0;
        rep(i, v.size()) tmp += 1.0 * v[i] / t[n - v.size() + i];
        ans = min(ans, tmp);
        c = true;
      }
    }

    if (n >= 8) {
      rep(v1, 30) loop(v2, v1 + 1, 30) loop(v3, v2 + 1, 30) loop(v4, v3 + 1, 30)
          loop(v5, v4 + 1, 30) loop(v6, v5 + 1, 30) loop(v7, v6 + 1, 30) {
        vi v;
        v.push_back(cost[a][v1]);
        v.push_back(cost[v1][v2]);
        v.push_back(cost[v2][v3]);
        v.push_back(cost[v3][v4]);
        v.push_back(cost[v4][v5]);
        v.push_back(cost[v5][v6]);
        v.push_back(cost[v6][v7]);
        v.push_back(cost[v7][b]);
        sort(all(v));
        if (v[0] <= 0 || n < v.size())
          continue;
        double tmp = 0;
        rep(i, v.size()) tmp += 1.0 * v[i] / t[n - v.size() + i];
        ans = min(ans, tmp);
        c = true;
      }
    }

    if (c) {
      cout << shosu(5) << ans << endl;
    } else {
      cout << "Impossible" << endl;
    }
  }
}