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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct node {
  int index, ticket;
  double cost;
  node(int index, int ticket, double cost)
      : index(index), ticket(ticket), cost(cost) {}
  bool operator<(const node &other) const { return cost > other.cost; }
};
const int MAX = INT_MAX / 10;
const int NONE = -1;
typedef vector<double> vd;
typedef vector<vd> vvd;
int main() {
  cout.precision(16);
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    vi tickets(n);
    REP(i, n) { cin >> tickets[i]; }
    vvi dist(m, vi(m, NONE));
    REP(i, p) {
      int x, y, z;
      cin >> x >> y >> z;
      dist[x - 1][y - 1] = z;
      dist[y - 1][x - 1] = z;
    }

    priority_queue<node> q;
    q.push(node(a - 1, 0, 0));
    vvd cost(m, vd((1 << n), MAX));
    cost[a - 1][0] = 0;
    while (!q.empty()) {
      node cnode = q.top();
      q.pop();
      if (cost[cnode.index][cnode.ticket] < cnode.cost) {
        continue;
      }
      REP(i, m) {
        if (i != cnode.index && dist[cnode.index][i] != NONE) {
          REP(j, n) {
            if (((1 << j) & cnode.ticket) == 0) {
              double newcost =
                  cnode.cost + (double)dist[cnode.index][i] / tickets[j];
              if (cost[i][(1 << j) | cnode.ticket] > newcost) {
                cost[i][(1 << j) | cnode.ticket] = newcost;
                q.push(node(i, (1 << j) | cnode.ticket, newcost));
              }
            }
          }
        }
      }
    }
    double ans = MAX;
    REP(i, 1 << n) { ans = min(ans, cost[b - 1][i]); }
    if (ans > MAX - 100) {
      cout << "Impossible" << endl;
    } else {
      cout << ans << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

const double MAX = INT_MAX / 10;
typedef vector<double> vd;
typedef vector<vd> vvd;

struct node {
  int cost, t, city;
  node(int cost, int t, int city) : cost(cost), t(t), city(city) {}
  bool operator<(const node &other) const { return cost > other.cost; }
};

int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    vi t(n);
    REP(i, n) { cin >> t[i]; }
    vvi path(m, vi(m, MAX));
    REP(i, p) {
      int x, y, z;
      cin >> x >> y >> z;
      path[x - 1][y - 1] = z;
      path[y - 1][x - 1] = z;
    }
    const int bit = 1 << t.size();
    vvd cost(bit, vd(m, MAX));
    priority_queue<node> q;
    q.push(node(0, 0, a - 1));
    cost[0][a - 1] = 0;
    vvi visited(bit, vi(m, 0));
    while (!q.empty()) {
      node p = q.top();
      q.pop();
      if (cost[p.t][p.city] < p.cost)
        continue;
      visited[p.t][p.city] = 1;
      REP(i, m) {
        if (path[p.city][i] != MAX) {
          REP(j, t.size()) {
            if (p.t & 1 << j)
              continue;
            double newcost = cost[p.t][p.city] + (double)path[p.city][i] / t[j];
            int newt = p.t | 1 << j;
            if (cost[newt][i] > newcost && !visited[newt][i]) {
              cost[newt][i] = newcost;
              q.push(node(newcost, newt, i));
            }
          }
        }
      }
    }
    double best = MAX;
    REP(i, bit) { best = min(best, cost[i][b - 1]); }
    if (best == MAX) {
      cout << "Impossible" << endl;
    } else {
      cout << best << endl;
    }
    cout.precision(16);
  }
}