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

#include <iostream>
#include <limits.h>
#include <queue>

using namespace std;

int n, m, p, a, b;

#define FOR(i, n) for (int i = 0; i < n; i++)

typedef struct {
  int x, y, z;
} LOAD;

typedef struct {
  double cost[256];
  bool used;
} CITY;

int ticket[8];
LOAD load[465];
CITY city[30 + 1]; // city[0]Ídummy

double get_min(double a, double b) { return (a > b) ? b : a; }

void search() {
  queue<int> city_nums;

  for (city_nums.push(a); !city_nums.empty(); city_nums.pop()) {
    int pipot = city_nums.front();
    if (pipot == b)
      continue; // ±±ÌoHÍIíè

    city[pipot].used =
        true; // ±±Íà¤Kê½±ÆÉÈé
    for (int j = 0; j < p; j++) {
      if ((load[j].x != pipot && load[j].y != pipot))
        continue; // ¹ðgÁ½©AÊêÈ¢
      int next =
          (load[j].x != pipot) ? load[j].x : load[j].y; // Â¬Ìcity
      if (city[next].used)
        continue;           // »Ì¬Í·ÅÉKê½
      city_nums.push(next); // ÉKêé¬
      // RXgvZ
      for (int i = 0; i < (1 << n); i++) {
        if (city[pipot].cost[i] == INT_MAX)
          continue; // »Ì`Pbg\\¬ÍÈ¢
        FOR(k, n) {
          if (i & (1 << k))
            continue; // kÔÚÍgÁÄ¢Ü·
          int new_tickets = i | (1 << k); // Vµ¢tO
          city[next].cost[new_tickets] =
              get_min(city[next].cost[new_tickets],
                      city[pipot].cost[i] +
                          load[j].z / (double)ticket[k]); // RXgvZ
        }
      }
    }
  }
}

void solve() {
  FOR(i, n)
  cin >> ticket[i];
  FOR(i, p)
  cin >> load[i].x >> load[i].y >> load[i].z;

  int flagNum = (1 << n);

  for (int i = 1; i <= m; i++) {
    city[i].used = false;
    FOR(j, flagNum)
    city[i].cost[j] = INT_MAX;
  }

  // gp`PbgÈµAcost0ÉÝè
  city[a].cost[0] = 0;
  search();

  double ans = (double)INT_MAX;
  FOR(i, flagNum)
  ans = get_min(ans, city[b].cost[i]);
  if (ans == (double)INT_MAX)
    cout << "Impossible" << endl;
  else
    cout << ans << endl;
}

int main() {
  while (cin >> n >> m >> p >> a >> b, (n || m || p || a || b)) {
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits.h>

using namespace std;

int n, m, p, a, b;

typedef struct {
  int n, used;
} TICKET;
typedef struct {
  int x, y, z;
  bool used;
} LOAD;

typedef struct {
  int a;
} CITY;

TICKET ticket[8];
LOAD load[465];
CITY city[30];

double min_cost;

double get_min(double a, double b) { return (a > b) ? b : a; }

void search(int a, double cost) {
  if (cost >= min_cost)
    return; // ツコツスツトツオツーツバツー

  if (a == b) {
    min_cost = get_min(min_cost, cost);
    return; // ツつアツつアツづ個経ツ路ツづ債終ツづュツづィ
  }

  for (int i = 0; i < n; i++) {
    if (ticket[i].used)
      continue;
    ticket[i].used = true;
    for (int j = 0; j < p; j++) {
      if (load[j].used || (load[j].x != a && load[j].y != a))
        continue; // ツ督ケツづーツ使ツづつスツつゥツ、ツ津環づェツづ按つ「
      load[j].used = true;
      search((load[j].x == a) ? load[j].y : load[j].x,
             cost + load[j].z / (double)ticket[i].n);
      load[j].used = false;
    }
    ticket[i].used = false;
  }
}

void solve() {
  for (int i = 0; i < n; i++)
    cin >> ticket[i].n, ticket[i].used = false;

  for (int i = 0; i < p; i++)
    cin >> load[i].x >> load[i].y >> load[i].z;

  for (int i = 0; i < m; i++) {
    city[i].a = i + 1;
  }

  min_cost = (double)INT_MAX;

  search(a, 0);

  if (min_cost == (double)INT_MAX)
    cout << "Impossible" << endl;
  else
    cout << min_cost << endl;
}

int main() {
  while (cin >> n >> m >> p >> a >> b, (n || m || p || a || b)) {
    solve();
  }

  return 0;
}