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

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << (S) << endl
#define ck(n, a, b) ((a) <= (n) && (a) < (b))
#define F first
#define S second
using namespace std;
const double inf = 1e9;
double dist[30][1 << 8];
int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    a--;
    b--;
    int t[8];
    REP(i, 0, n) cin >> t[i];
    vector<pair<int, int>> edge[30]; // dist,node
    REP(i, 0, p) {
      int x, y, z;
      cin >> x >> y >> z;
      x--;
      y--;
      edge[x].push_back({z, y});
      edge[y].push_back({z, x});
    }
    REP(i, 0, m) REP(j, 0, (1 << n)) dist[i][j] = inf;
    priority_queue<pair<double, pair<int, int>>> pq; // dist,baken,node
    pq.push({0, {0, a}});
    dist[a][0] = 0;
    // Dijkstra
    while (!pq.empty()) {
      auto now = pq.top();
      pq.pop();
      double nd = -now.F;
      int nb = now.S.F, nn = now.S.S;
      if (nd > dist[nn][nb])
        continue;
      if (nb == (1 << n) - 1)
        continue;
      for (auto e : edge[nn]) {
        double nxd = e.F;
        int nxn = e.S;
        REP(i, 0, n) {
          if (nb & (1 << i))
            continue;
          if (dist[nxn][nb | (1 << i)] > nd + nxd / t[i]) {
            dist[nxn][nb | (1 << i)] = nd + nxd / t[i];
            pq.push({-dist[nxn][nb | (1 << i)], {nb | (1 << i), nxn}});
          }
        }
      }
    }
    double mn = inf;
    REP(i, 0, (1 << n)) mn = min(mn, dist[b][i]);
    if (mn == inf) {
      p("Impossible");
      continue;
    }
    printf("%.4lf\\n", mn);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << (S) << endl
#define ck(n, a, b) ((a) <= (n) && (a) < (b))
#define F first
#define S second
using namespace std;
const double inf = 1e9;
double dist[31][1 << 8];
int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    a--;
    b--;
    int t[8];
    REP(i, 0, n) cin >> t[i];
    vector<pair<int, int>> edge[31]; // dist,node
    REP(i, 0, p) {
      int x, y, z;
      cin >> x >> y >> z;
      x--;
      y--;
      edge[x].push_back({z, y});
      edge[y].push_back({z, x});
    }
    REP(i, 0, m) REP(j, 0, (1 << n)) dist[i][j] = inf;
    priority_queue<pair<double, pair<int, int>>> pq; // dist,baken,node
    pq.push({0, {0, a}});
    dist[a][0] = 0;
    // Dijkstra
    while (!pq.empty()) {
      auto now = pq.top();
      pq.pop();
      double nd = -now.F;
      int nb = now.S.F, nn = now.S.S;
      if (nd > dist[nn][nb])
        continue;
      if (nb == (1 << n) - 1)
        continue;
      for (auto e : edge[nn]) {
        double nxd = e.F;
        int nxn = e.S;
        REP(i, 0, n) {
          if (nb & (1 << i))
            continue;
          if (dist[nxn][nb | (1 << i)] >= nd + nxd / t[i]) {
            dist[nxn][nb | (1 << i)] = nd + nxd / t[i];
            pq.push({-dist[nxn][nb | (1 << i)], {nb | (1 << i), nxn}});
          }
        }
      }
    }
    double mn = inf;
    REP(i, 0, (1 << n)) mn = min(mn, dist[b][i]);
    if (mn == inf) {
      p("Impossible");
      continue;
    }
    printf("%.4lf\\n", mn);
  }

  return 0;
}