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

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

#define MAX_NODE_ID 31
#define MAX_TICKET_NUM 10
#define MAX_TICKET 256
class Node {
public:
  int id;
  double cost;
  int ticketStatus;
  Node(int i, double c, int t) {
    id = i;
    cost = c;
    ticketStatus = t;
  }

  bool operator<(const Node &o) const { return cost > o.cost; }
};
int table[MAX_NODE_ID][MAX_NODE_ID];
bool visited[MAX_NODE_ID][MAX_TICKET];
int t[10];
double costPos[MAX_NODE_ID][MAX_TICKET];
typedef priority_queue<Node> PQ;
int main() {
  // input
  for (int n, m, p, a, b; cin >> n >> m >> p >> a >> b && n;) {
    int j, k;
    // ツ渉可甘コツ可サ
    for (j = 0; j < MAX_NODE_ID; j++)
      for (k = 0; k < MAX_NODE_ID; k++)
        table[j][k] = 0;
    for (j = 0; j < MAX_NODE_ID; j++)
      for (k = 0; k < MAX_TICKET; k++) {
        visited[j][k] = false;
        costPos[j][k] = 0;
      }
    for (j = 0; j < n && cin >> t[j]; j++)
      ;

    int x, y, z;
    for (j = 0; j < p && cin >> x >> y >> z; j++) {
      table[x][y] = z;
      table[y][x] = z;
    }
    PQ pq;
    pq.push(Node(a, 0, 0.0));
    while (!pq.empty()) {
      int curId = pq.top().id;
      double curCost = pq.top().cost;
      int curTicket = pq.top().ticketStatus;

      if (curId == b) {
        break;
      }
      pq.pop();
      if (visited[curId][curTicket] || curTicket == (1 << n) - 1) {
        continue;
      }

      for (j = 1; j <= m; j++) {
        if (table[curId][j] == 0)
          continue;
        for (k = 0; k < n; k++) {
          int _ticket = 1 << k;
          if (!(curTicket & _ticket)) {
            visited[curId][curTicket] = true;
            double cost = curCost + (double)table[curId][j] / (double)t[k];
            int ticketStatus = curTicket + _ticket;
            if (costPos[curId][ticketStatus] <
                (double)table[curId][j] / (double)t[k]) {
              Node next1(j, cost, ticketStatus);
              pq.push(next1);
            }
          }
        }
      }
    }
    if (pq.top().id == b)
      printf("%lf\\n", pq.top().cost);
    else
      printf("Impossible\\n");
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

#define MAX_NODE_ID 31
#define MAX_TICKET_NUM 10
#define MAX_TICKET 256
class Node {
public:
  int id;
  double cost;
  int ticketStatus;
  Node(int i, double c, int t) {
    id = i;
    cost = c;
    ticketStatus = t;
  }

  bool operator<(const Node &o) const { return cost > o.cost; }
};
int table[MAX_NODE_ID][MAX_NODE_ID];
bool visited[MAX_NODE_ID][MAX_TICKET];
int t[10];
double costPos[MAX_NODE_ID][MAX_TICKET];
typedef priority_queue<Node> PQ;
int main() {
  // input
  for (int n, m, p, a, b; cin >> n >> m >> p >> a >> b && n;) {
    int j, k;
    // ツ渉可甘コツ可サ
    for (j = 0; j < MAX_NODE_ID; j++)
      for (k = 0; k < MAX_NODE_ID; k++)
        table[j][k] = 0;
    for (j = 0; j < MAX_NODE_ID; j++)
      for (k = 0; k < MAX_TICKET; k++) {
        visited[j][k] = false;
        costPos[j][k] = 0;
      }
    for (j = 0; j < n && cin >> t[j]; j++)
      ;

    int x, y, z;
    for (j = 0; j < p && cin >> x >> y >> z; j++) {
      table[x][y] = z;
      table[y][x] = z;
    }
    PQ pq;
    pq.push(Node(a, 0, 0.0));
    while (!pq.empty()) {
      int curId = pq.top().id;
      double curCost = pq.top().cost;
      int curTicket = pq.top().ticketStatus;

      if (curId == b)
        break;
      pq.pop();
      if (visited[curId][curTicket - 1] || curTicket == (1 << n) - 1)
        continue;
      for (j = 1; j <= m; j++) {
        if (table[curId][j] == 0)
          continue;
        for (k = 1; k <= n; k++) {
          int _ticket = 1 << k;
          if (!(curTicket & _ticket)) {
            visited[curId][2 << (k - 1)] = true;
            double cost = curCost + (double)table[curId][j] / (double)t[k - 1];
            int ticketStatus = curTicket + _ticket;
            if (costPos[curId][ticketStatus] <
                (double)table[curId][j] / (double)t[k - 1]) {
              Node next1(j, cost, ticketStatus);
              pq.push(next1);
            }
          }
        }
      }
    }
    if (pq.top().id == b)
      printf("%lf\\n", pq.top().cost);
    else
      printf("Impossible\\n");
  }
  return 0;
}