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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

double solve(int tn, int cn, double t[8], double r[31][31], int s, int e) {
  double result[(1 << tn)][cn + 1];
  // result[i][j] :
  // i???????????°??¨?¨???????1????????£???????????¨???????????¬???????¶?????????????j???????????????????????????????????¢
  for (int i = 0; i < (1 << tn); i++) {
    for (int j = 1; j <= cn; j++) {
      result[i][j] = mod + 0.0;
    }
  }
  result[0][s] = 0.0;
  for (int i = 0; i < (1 << tn); i++) {
    for (int j = 1; j <= cn; j++) {
      // cout << result[i][j] << " ";
      // ???????????????j????????????????????£?????¬???????????±???i
      // ?????????????????¬???????±???????
      for (int k = 0; k < tn; k++) {
        if ((i & (1 << k)) != 0)
          continue;
        // ??????????????????????±???????
        for (int l = 1; l <= cn; l++) {
          if (r[j][l] == mod)
            continue;
          result[(i | (1 << k))][l] =
              min(result[(i | (1 << k))][l], result[i][j] + r[j][l] / t[k]);
        }
      }
    }
    // cout << endl;
  }
  double ans = mod + 0.0;
  for (int i = 0; i < (1 << tn); i++) {
    ans = min(ans, result[i][e]);
  }
  return ans;
}

int main() {
  int n, m, p, a, b;
  while (1) {
    cin >> n >> m >> p >> a >> b;
    if (n == 0)
      break;
    double tichet[8];
    double road[31][31];
    for (int i = 0; i < n; i++) {
      cin >> tichet[i];
    }
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= m; j++) {
        road[i][j] = mod + 0.0;
      }
    }
    for (int i = 0; i < p; i++) {
      int x, y;
      double z;
      cin >> x >> y >> z;
      road[x][y] = z;
      road[y][x] = z;
    }
    double ans = solve(n, m, tichet, road, a, b);
    if (ans == mod)
      cout << "Impossible" << endl;
    else
      printf("%.3f\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int n, m, p, a, b;
int t[9];
int road[31][31];
bool ischecked[31];

double ans;

void solve(vector<int> cost, int nowtown) {
  // cout << nowtown << endl;
  if (cost.size() > n)
    return;
  if (cost.size() <= n && nowtown == b) {
    sort(cost.begin(), cost.end());
    reverse(cost.begin(), cost.end());
    double result = 0.0;
    for (int i = 0; i < cost.size(); i++) {
      result += (double)cost[i] / t[i];
    }
    ans = min(ans, result);
    return;
  }
  for (int i = 0; i < m; i++) {
    if (road[nowtown][i] != -1 && !ischecked[i]) {
      cost.push_back(road[nowtown][i]);
      ischecked[i] = true;
      solve(cost, i);
      cost.pop_back();
      ischecked[i] = false;
    }
  }
  return;
}

int main() {
  while (1) {
    cin >> n >> m >> p >> a >> b;
    a--;
    b--;
    if (n == 0)
      break;
    for (int i = 0; i < 31; i++) {
      ischecked[i] = false;
      for (int j = 0; j < 31; j++) {
        road[i][j] = -1;
      }
    }
    ans = 100000000.0;
    for (int i = 0; i < n; i++) {
      cin >> t[i];
    }
    sort(t, t + n);
    reverse(t, t + n);
    for (int i = 0; i < p; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      x--;
      y--;
      road[x][y] = z;
      road[y][x] = z;
    }
    vector<int> cost;
    ischecked[a] = true;
    solve(cost, a);
    if (ans == 100000000.0)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}