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

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define INF (1 << 21)
using namespace std;

class Point {
public:
  int n, t;
  double c;
  bool f;
  Point(int n = 0, int t = 0, double c = 0, bool f = false)
      : n(n), t(t), c(c), f(f){};
  bool operator<(const Point &p) const { return c > p.c; }
};

int main() {
  while (1) {
    int i, j, n, m, p, s, g, tmax;
    double G[31][31], t[10];
    Point d[31][256], u, v;

    cin >> n >> m >> p >> s >> g;
    if (!n && !m && !p && !s && !g)
      break;

    for (i = 1; i <= m; i++) {
      for (j = 1; j <= m; j++)
        G[i][j] = INF;
    }
    tmax = (int)pow(2.0, (double)n);
    for (i = 1; i <= m; i++) {
      for (j = 0; j < tmax; j++) {
        d[i][j].c = INF;
        d[i][j].n = i;
        d[i][j].t = j;
      }
    }

    for (i = 0; i < n; i++)
      cin >> t[i];
    for (i = 0; i < p; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      G[x][y] = G[y][x] = (double)z;
    }

    priority_queue<Point> Q;
    d[s][0].c = 0.0;
    Q.push(d[s][0]);
    while (!Q.empty()) {
      u = Q.top();
      Q.pop();

      d[u.n][u.t].f = true;

      for (j = 0; j < n; j++) {
        int x = (1 << j);

        if ((u.t & x) == 0) {
          v = u;
          v.t = (u.t | x);

          for (i = 1; i <= m; i++)
            d[i][v.t].f = d[i][u.t].f;

          for (i = 1; i <= m; i++) {
            if (!d[i][v.t].f && (G[v.n][i] / t[j]) + u.c < d[i][v.t].c &&
                G[v.n][i] != INF) {
              d[i][v.t].c = (G[v.n][i] / t[j]) + u.c;
              Q.push(d[i][v.t]);
            }
          }
        }
      }
    }

    double ans = INF;
    for (i = 0; i < tmax; i++)
      ans = min(ans, d[g][i].c);

    if (ans == INF)
      cout << "Impossible" << endl;
    else
      printf("%.6f\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define INF (1 << 21)
using namespace std;

class Point {
public:
  int n, t;
  double c;
  bool f;
  Point(int n = 0, int t = 0, double c = 0, bool f = false)
      : n(n), t(t), c(c), f(f){};
  bool operator<(const Point &p) const { return c > p.c; }
};

int main() {
  while (1) {
    int i, j, n, m, p, s, g, tmax;
    double G[31][31], t[10];
    Point d[31][256], u, v;

    cin >> n >> m >> p >> s >> g;
    if (!n && !m && !p && !s && !g)
      break;

    for (i = 1; i <= m; i++) {
      for (j = 1; j <= m; j++)
        G[i][j] = INF;
    }
    tmax = (int)pow(2.0, (double)n);
    for (i = 1; i <= m; i++) {
      for (j = 0; j < tmax; j++) {
        d[i][j].c = INF;
        d[i][j].n = i;
        d[i][j].t = j;
      }
    }

    for (i = 0; i < n; i++)
      cin >> t[i];
    for (i = 0; i < p; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      G[x][y] = G[y][x] = (double)z;
    }

    priority_queue<Point> Q;
    d[s][0].c = 0.0;
    Q.push(d[s][0]);
    while (!Q.empty()) {
      u = Q.top();
      Q.pop();

      if (d[u.n][u.t].c < u.c)
        continue;

      d[u.n][u.t].f = true;

      for (i = 1; i <= m; i++) {
        for (j = 0; j < n; j++) {
          int x = (1 << j);

          if ((u.t & x) == 0) {
            v = u;
            v.t = (u.t | x);

            if (!d[i][v.t].f && (G[v.n][i] / t[j]) + u.c < d[i][v.t].c &&
                G[v.n][i] != INF) {
              d[i][v.t].c = (G[v.n][i] / t[j]) + u.c;
              Q.push(d[i][v.t]);
            }
          }
        }
      }
    }

    double ans = INF;
    for (i = 0; i < tmax; i++)
      ans = min(ans, d[g][i].c);

    if (ans == INF)
      cout << "Impossible" << endl;
    else
      printf("%.6f\\n", ans);
  }
  return 0;
}