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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define INF 10000

typedef double Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int n, m, p, a, b;
int ti[8];

typedef pair<Edge, int> P;

Weight dist[1 << 8][30];
bool visited[1 << 8][30];

Weight dijkstra(const Graph &g, int s, int t) {
  REP(i, 1 << n) REP(j, g.size()) {
    dist[i][j] = INF;
    visited[i][j] = 0;
  }
  dist[0][s] = 0;
  int ma = 0;
  priority_queue<P> Q; // "e < f" <=> "e.weight > f.weight"
  for (Q.push(P(Edge(-2, s, 0), 0)); !Q.empty();) {
    Edge e = Q.top().first;
    int S = Q.top().second;
    Q.pop();
    if (t == e.dst)
      return dist[S][e.dst];
    if (dist[S][e.dst] < e.weight)
      continue;
    if (visited[S][e.dst])
      continue;
    visited[S][e.dst] = 1;
    FOR(f, g[e.dst]) {
      REP(i, n) {
        if ((S >> i) & 1)
          continue;
        if (visited[S | 1 << i][f->dst])
          continue;
        double cost = e.weight + f->weight / ti[i];
        if (dist[S | (1 << i)][f->dst] > cost) {
          dist[S | (1 << i)][f->dst] = cost;
          Q.push(P(Edge(f->src, f->dst, cost), S | (1 << i)));
        }
      }
    }
    ma = max(ma, (int)Q.size());
  }
  // cout << ma << endl;
  return INF;
}

int main() {
  while (cin >> n >> m >> p >> a >> b, n) {
    //    cout << n <<m<<p<< endl;
    a--;
    b--;
    REP(i, n) cin >> ti[i];
    Graph g(m);
    REP(i, p) {
      int x, y, z;
      cin >> x >> y >> z;
      x--;
      y--;
      g[x].push_back(Edge(x, y, z));
      g[y].push_back(Edge(y, x, z));
    }
    double res = dijkstra(g, a, b);
    if (res == INF)
      cout << "Impossible" << endl;
    else
      printf("%.8lf\\n", res);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef double Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int n, m, p, a, b;
int ti[8];

typedef pair<Edge, int> P;

Weight dist[1 << 8][30];

Weight dijkstra(const Graph &g, int s, int t) {
  REP(i, 1 << n) REP(j, g.size()) dist[i][j] = INF;
  dist[0][s] = 0;
  int ma = 0;
  priority_queue<P> Q; // "e < f" <=> "e.weight > f.weight"
  for (Q.push(P(Edge(-2, s, 0), 0)); !Q.empty();) {
    Edge e = Q.top().first;
    int S = Q.top().second;
    Q.pop();
    if (t == e.dst)
      return dist[S][e.dst];
    if (dist[S][e.dst] < e.weight)
      continue;
    FOR(f, g[e.dst]) {
      REP(i, n) {
        if ((S >> i) & 1)
          continue;
        double cost = e.weight + f->weight / ti[i];
        if (dist[S | (1 << i)][f->dst] > cost) {
          dist[S | (1 << i)][f->dst] = cost;
          Q.push(P(Edge(f->src, f->dst, cost), S | (1 << i)));
        }
      }
    }
    ma = max(ma, (int)Q.size());
  }
  //  cout << ma << endl;
  return INF;
}

int main() {
  while (cin >> n >> m >> p >> a >> b, n) {
    cout << n << m << p << endl;
    a--;
    b--;
    REP(i, n) cin >> ti[i];
    Graph g(m);
    REP(i, p) {
      int x, y, z;
      cin >> x >> y >> z;
      x--;
      y--;
      g[x].push_back(Edge(x, y, z));
      g[y].push_back(Edge(y, x, z));
    }
    double res = dijkstra(g, a, b);
    if (res == INF)
      cout << "Impossible" << endl;
    else
      printf("%.8lf\\n", res);
  }
}