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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second

using namespace std;

const double INF = 1e9;

int N, M, P, A, B;
double t[10], road[35][35], dp[35][(1 << 8)];
int testCnt = 0;

double dfs(int nowNode, int ticketState) {
  if (nowNode == B - 1)
    return 0;
  if (ticketState == 0)
    return INF;
  if (dp[nowNode][ticketState] != -1.0)
    return dp[nowNode][ticketState];
  double ret = INF;
  // bool flag = false;
  REP(nextNode, 0, M) {
    if (road[nowNode][nextNode] == INF)
      continue;
    REP(ticket, 0, N) {
      if (!(ticketState & (1 << ticket)))
        continue;
      ret = min(ret, dfs(nextNode, ticketState ^ (1 << ticket)) +
                         road[nowNode][nextNode] / t[ticket]);
    }
  }
  testCnt++;
  return dp[nowNode][ticketState] = ret;
}

int main() {
  while (1) {
    cin >> N >> M >> P >> A >> B;
    if (N == 0)
      break;
    REP(i, 0, N) cin >> t[i];
    REP(i, 0, M) REP(j, 0, M) road[i][j] = INF;
    REP(i, 0, P) {
      int x, y;
      double z;
      cin >> x >> y >> z;
      x--;
      y--;
      road[x][y] = road[y][x] = z;
    }
    REP(i, 0, M) REP(j, 0, (1 << N)) dp[i][j] = -1.0;
    double ans = dfs(A - 1, (1 << N) - 1);
    if (ans < INF)
      printf("%.5lf\\n", ans);
    else
      cout << "Impossible" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second

using namespace std;

const double INF = 1e9;

int N, M, P, A, B;
double t[10], road[35][35], dp[35][(1 << 8)];

double dfs(int nowNode, int ticketState) {
  if (nowNode == B - 1)
    return 0;
  if (ticketState == 0)
    return INF;
  if (dp[nowNode][ticketState] != INF)
    return dp[nowNode][ticketState];
  double ret = INF;
  REP(nextNode, 0, M) {
    if (road[nowNode][nextNode] == INF)
      continue;
    REP(ticket, 0, N) {
      if (!(ticketState & (1 << ticket)))
        continue;
      ret = min(ret, dfs(nextNode, ticketState ^ (1 << ticket)) +
                         road[nowNode][nextNode] / t[ticket]);
    }
  }
  return dp[nowNode][ticketState] = ret;
}

int main() {
  while (1) {
    cin >> N >> M >> P >> A >> B;
    if (N == 0)
      break;
    REP(i, 0, N) cin >> t[i];
    REP(i, 0, M) REP(j, 0, M) road[i][j] = INF;
    REP(i, 0, P) {
      int x, y;
      double z;
      cin >> x >> y >> z;
      x--;
      y--;
      road[x][y] = road[y][x] = z;
    }
    REP(i, 0, M) REP(j, 0, (1 << N)) dp[i][j] = INF;
    double ans = dfs(A - 1, (1 << N) - 1);
    if (ans < INF)
      printf("%.5lf\\n", ans);
    else
      cout << "Impossible" << endl;
  }
  return 0;
}