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

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

#define INF 10000
#define MAXN 8
#define MAXM 30

int T[MAXN];
int G[MAXM][MAXM];
double dp[MAXM][1 << MAXN];
bool used[MAXM][1 << MAXN];
int n, m, p, a, b;

double dijkstra() {
  for (int i = 0; i < MAXM; i++) {
    for (int j = 0; j < (1 << MAXN); j++) {
      dp[i][j] = INF;
      used[i][j] = false;
    }
  }
  dp[a][(1 << n) - 1] = 0;

  while (true) {
    int v = -1;
    int w;
    for (int u = 0; u < m; u++) {
      for (int s = 0; s < (1 << n); s++) {
        if (!used[u][s] && (v == -1 || dp[u][s] < dp[v][s])) {
          v = u;
          w = s;
        }
      }
    }
    if (v == -1) {
      break;
    }
    used[v][w] = true;
    for (int u = 0; u < m; u++) {
      for (int i = 0; i < n; i++) {
        if (G[v][u] == INF) {
          continue;
        }
        if (w >> i & 1) {
          dp[u][w & ~(1 << i)] =
              min(dp[u][w & ~(1 << i)], dp[v][w] + (double)G[v][u] / T[i]);
        }
      }
    }
  }

  double ans = INF;
  for (int i = 0; i < (1 << n); i++) {
    ans = min(ans, dp[b][i]);
  }
  return ans;
}

int main() {
  int x, y, z;
  while (true) {
    cin >> n >> m >> p >> a >> b;
    if (n == 0 && m == 0) {
      break;
    }
    a--;
    b--;
    for (int i = 0; i < MAXM; i++) {
      for (int j = 0; j < MAXM; j++) {
        G[i][j] = INF;
      }
    }
    for (int i = 0; i < MAXM; i++) {
      G[i][i] = 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> T[i];
    }
    for (int i = 0; i < p; i++) {
      cin >> x >> y >> z;
      x--;
      y--;
      G[x][y] = G[y][x] = z;
    }
    double res = dijkstra();
    if (res == INF) {
      cout << "Impossible" << endl;
    } else {
      printf("%.3f\\n", res);
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

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

#define INF 10000
#define MAXN 20
#define MAXM 8

int T[MAXN];
int G[MAXM][MAXM];
double dp[MAXM][1 << MAXN];
bool used[MAXM][1 << MAXN];
int n, m, p, a, b;

double dijkstra() {
  for (int i = 0; i < MAXM; i++) {
    for (int j = 0; j < (1 << MAXN); j++) {
      dp[i][j] = INF;
      used[i][j] = false;
    }
  }
  dp[a][(1 << n) - 1] = 0;

  while (true) {
    int v = -1;
    int w;
    for (int u = 0; u < m; u++) {
      for (int s = 0; s < (1 << n); s++) {
        if (!used[u][s] && (v == -1 || dp[u][s] < dp[v][s])) {
          v = u;
          w = s;
        }
      }
    }
    if (v == -1) {
      break;
    }
    used[v][w] = true;
    for (int u = 0; u < m; u++) {
      for (int i = 0; i < n; i++) {
        if (G[v][u] == INF) {
          continue;
        }
        if (w >> i & 1) {
          dp[u][w & ~(1 << i)] =
              min(dp[u][w & ~(1 << i)], dp[v][w] + (double)G[v][u] / T[i]);
        }
      }
    }
  }
  //    for (int i = 0; i < m; i++) {
  //        for (int j = 0; j < (1<<n); j++) {
  //            cout << dp[i][j] <<" ";
  //        }
  //        cout << endl;
  //    }
  double ans = INF;
  for (int i = 0; i < (1 << n); i++) {
    ans = min(ans, dp[b][i]);
  }
  return ans;
}

int main() {
  int x, y, z;
  while (true) {
    cin >> n >> m >> p >> a >> b;
    if (n == 0 && m == 0) {
      break;
    }
    a--;
    b--;
    for (int i = 0; i < MAXM; i++) {
      for (int j = 0; j < MAXM; j++) {
        G[i][j] = INF;
      }
    }
    for (int i = 0; i < MAXM; i++) {
      G[i][i] = 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> T[i];
    }
    for (int i = 0; i < p; i++) {
      cin >> x >> y >> z;
      x--;
      y--;
      G[x][y] = G[y][x] = z;
    }
    double res = dijkstra();
    if (res == INF) {
      cout << "Impossible" << endl;
    } else {
      printf("%.3f\\n", res);
    }
  }

  return 0;
}