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

#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define EPS 1.0e-5
using namespace std;

class P {
public:
  short to, dis;
  P();
  P(int t, int d) {
    to = t;
    dis = d;
  }
};
class S {
public:
  char m;      // 現在地
  short st;    // 馬車券の使用状態
  double time; // 所要時間
  S();
  S(int M, int ST, double t) {
    m = M;
    st = ST;
    time = t;
  };
  bool operator<(const S &s) const { return time > s.time; }
};

int main() {
  int n, m, p, a, b, x, y, z;
  int t[8];
  vector<P> v[30];
  priority_queue<S> que;
  double min[30][1 << 8]; // 最小経過時間

  while (cin >> n >> m >> p >> a >> b && (n | m | p | a | b)) {
    --a;
    --b;
    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int i = 0; i < m; i++)
      v[i].clear();
    for (int i = 0; i < p; i++) {
      cin >> x >> y >> z;
      --x;
      --y; // 0-indexed
      v[x].push_back(P(y, z));
      v[y].push_back(P(x, z));
    }
    for (int i = 0; i < m; i++)
      for (int j = 0, k = 1 << n; j < k; j++)
        min[i][j] = DBL_MAX;

    while (!que.empty())
      que.pop();

    min[a][0] = 0;
    que.push(S(a, 0, 0.0));
    double ans = DBL_MAX;
    while (!que.empty()) {
      S s = que.top();
      que.pop();

      if (s.m == b) {
        ans = s.time;
        break;
      }
      if (min[s.m][s.st] < s.time)
        continue;

      for (unsigned int i = 0; i < v[s.m].size(); i++) {
        P p = v[s.m][i];
        if (p.to == s.m)
          continue;
        for (int j = 0; j < n; j++) {
          int nState = s.st | (1 << j);
          if (nState == s.st)
            continue;
          double nCost = s.time + (double)p.dis / t[j];
          if (abs(nCost - min[p.to][nState]) < EPS)
            continue;
          if (nCost < min[p.to][nState]) {
            que.push(S(p.to, nState, nCost));
            min[p.to][nState] = nCost;
          }
        }
      }
    }
    if (fabs(ans - DBL_MAX) < EPS)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class P {
public:
  int to, dis;
  P();
  P(int t, int d) {
    to = t;
    dis = d;
  }
};
class S {
public:
  char m;      // 現在地
  short st;    // 馬車券の使用状態
  double time; // 所要時間
  S();
  S(int M, int ST, double t) {
    m = M;
    st = ST;
    time = t;
  };
  bool operator<(const S &s) const { return time > s.time; }
};

int main() {
  int n, m, p, a, b, x, y, z;
  int t[8];
  vector<P> v[30];
  priority_queue<S> que;
  double min[30][1 << 8]; // 最小経過時間

  while (cin >> n >> m >> p >> a >> b && (n | m | p | a | b)) {
    --a;
    --b;
    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int i = 0; i < m; i++)
      v[i].clear();
    for (int i = 0; i < p; i++) {
      cin >> x >> y >> z;
      --x;
      --y; // 0-indexed
      v[x].push_back(P(y, z));
      v[y].push_back(P(x, z));
    }
    for (int i = 0; i < m; i++)
      for (int j = 0, k = 1 << n; j < k; j++)
        min[i][j] = DBL_MAX;

    while (!que.empty())
      que.pop();

    min[a][0] = 0;
    que.push(S(a, 0, 0.0));
    double ans = DBL_MAX;
    while (!que.empty()) {
      S s = que.top();
      que.pop();

      if (s.m == b) {
        ans = s.time;
        break;
      }
      if (min[s.m][s.st] < s.time)
        continue;

      for (unsigned int i = 0; i < v[s.m].size(); i++) {
        P p = v[s.m][i];
        if (p.to == s.m)
          continue;
        for (int j = 0; j < n; j++) {
          int nState = s.st | (1 << j);
          if (nState == s.st)
            continue;
          double nCost = s.time + (double)p.dis / t[j];
          if (nCost < min[p.to][nState]) {
            que.push(S(p.to, nState, nCost));
            min[p.to][nState] = nCost;
          }
        }
      }
    }
    if (fabs(ans - DBL_MAX) < 1.0e-5)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}