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

#include <cstdio>
#include <cstring>
#include <iostream>
#define INF 100000000
#include <map>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> q;
typedef pair<double, q> P; // vAêA`P
/*struct edge{
        int to,cost;
};
*/
int main() {
  int a, b, n, m, p, x, y, z, t[15];
  while (cin >> n >> m >> p >> a >> b, n || m || p || a || b) {
    priority_queue<P, vector<P>, greater<P>> que;
    a--;
    double d[1 << 8][31];
    int vis[1 << 8][31]; // `PAê
    b--;                 // vector<edge> v[40];
    int v[31][31];
    for (int i = 0; i < m; i++)
      fill(v[i], v[i] + m, INF);
    memset(vis, -1, sizeof(vis));
    rep(i, n) cin >> t[i];
    rep(i, p) {
      cin >> x >> y >> z;
      x--;
      y--;
      if (v[x][y] > z)
        v[x][y] = z;
      if (v[y][x] > z)
        v[y][x] = z;
    }
    rep(i, 1 << n) rep(j, m) {
      if (j == a)
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
    que.push(P(0, q(a, 0)));
    int now, tic;
    P e;
    double sum;
    while (!que.empty()) {
      e = que.top();
      que.pop();
      now = e.second.first;
      sum = e.first;
      tic = e.second.second;
      if (d[tic][now] < sum || vis[tic][now] != -1) {
        continue;
      }
      vis[tic][now] = 1;
      if (now == b)
        break;
      d[tic][now] = sum;
      rep(i, m) {
        int w = v[now][i];
        if (w == INF)
          continue;

        // edge w=v[now][i];
        rep(j, n) {
          if (!(tic & (1 << j))) {
            if (d[tic | (1 << j)][i] > sum + (double)w / t[j]) {
              d[tic | (1 << j)][i] = sum + (double)w / t[j];
              que.push(P(d[tic | (1 << j)][i], q(i, tic | (1 << j))));
            }
          }
        }
      }
    }
    double ans = INF;
    rep(i, 1 << n) ans = min(ans, d[i][b]);
    if (ans == INF)
      cout << "Impossible" << endl;
    else
      printf("%.3lf\\n", ans);
    // rep(i,m)
    // v[i].clear();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define INF 100000000
#include <map>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> q;
typedef pair<double, q> P; // vAêA`P
/*struct edge{
        int to,cost;
};
*/
int main() {

  int a, b, n, m, p, x, y, z, t[15];
  while (cin >> n >> m >> p >> a >> b, n || m || p || a || b) {
    priority_queue<P, vector<P>, greater<P>> que;
    a--;
    double d[1 << 8][31]; // `PAê
    b--;                  // vector<edge> v[40];
    int v[31][31];
    for (int i = 0; i < m; i++)
      fill(v[i], v[i] + m, INF);
    rep(i, n) cin >> t[i];
    rep(i, p) {
      cin >> x >> y >> z;
      x--;
      y--;
      if (v[x][y] > z)
        v[x][y] = z;
      if (v[y][x] > z)
        v[y][x] = z;
    }
    rep(i, 1 << n) rep(j, m) {
      if (j == a)
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
    que.push(P(0, q(a, 0)));
    int now, tic;
    P e;
    double sum;
    while (!que.empty()) {
      e = que.top();
      que.pop();
      now = e.second.first;
      sum = e.first;
      tic = e.second.second;
      if (d[tic][now] < sum) {
        continue;
      }
      if (now == b)
        break;
      d[tic][now] = sum;
      rep(i, m) {
        int w = v[now][i];
        if (w == INF)
          continue;
        // edge w=v[now][i];
        rep(j, n) {
          if (!(tic & (1 << j))) {
            if (d[tic | (1 << j)][i] > sum + (double)w / t[j]) {
              d[tic | (1 << j)][i] = sum + (double)w / t[j];
              que.push(P(d[tic | (1 << j)][i], q(i, tic | (1 << j))));
            }
          }
        }
      }
    }
    double ans = INF;
    rep(i, 1 << n) ans = min(ans, d[i][b]);
    if (ans == INF)
      cout << "Impossible" << endl;
    else
      printf("%.3lf\\n", ans);
    // rep(i,m)
    // v[i].clear();
  }
  return 0;
}