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

// g++ -std=c++11 temp.cpp
#include <bits/stdc++.h>
using namespace std;

#define For(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Rep(i, n) For(i, 0, n)
#define Each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;

#define INF INT_MAX / 3

#define MAX_N 8
#define MAX_M 30

int n, m, a, b, p;
int t[MAX_N];
int d[MAX_M][MAX_M];

double dp[1 << MAX_N][MAX_M];

void solve() {
  Rep(i, 1 << n) fill(dp[i], dp[i] + m, INF);
  dp[(1 << n) - 1][a] = 0;
  double res = INF;
  for (int S = (1 << n) - 1; S >= 0; S--) {
    res = min(res, dp[S][b]);
    Rep(v, m) Rep(i, n) {
      if (S >> i & 1) {
        Rep(u, m) {
          if (d[v][u] >= 0) {
            dp[S & ~(1 << i)][u] =
                min(dp[S & ~(1 << i)][u], dp[S][v] + (double)d[v][u] / t[i]);
          }
        }
      }
    }
  }
  if (res == INF) {
    printf("Impossible\\n");
  } else {
    printf("%.3f\\n", res);
  }
}

int main() {
  while (1) {
    scanf("%d %d %d %d %d", &n, &m, &p, &a, &b);
    if (n == 0)
      break;
    a--;
    b--;
    Rep(i, n) { scanf("%d", &t[i]); }
    Rep(i, m) Rep(j, m) {
      d[i][j] = -1;
      d[j][i] = -1;
    }
    Rep(i, p) {
      int x, y, z;
      scanf("%d %d %d", &x, &y, &z);
      x--;
      y--;
      d[x][y] = z;
      d[y][x] = z;
    }
    solve();
  }
  return 0;
}

Error: MLE

Fixed submission:

// g++ -std=c++11 temp.cpp
#include <bits/stdc++.h>
using namespace std;

#define For(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Rep(i, n) For(i, 0, n)
#define Each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;

#define INF INT_MAX / 3

#define MAX_M 30

struct edge {
  int to, len;
  edge(int a, int b) : to(a), len(b) {}
};

struct state {
  int c, prec;
  vector<int> road;
};

int n, m, p, a, b;
vector<int> t;
vector<edge> G[30];
double ans = INF;

int main() {
  while (1) {
    scanf("%d %d %d %d %d", &n, &m, &p, &a, &b);
    if (n == 0)
      break;
    a--;
    b--;
    t.clear();
    Rep(i, n) {
      int t1;
      scanf("%d", &t1);
      t.pb(t1);
    }
    sort(all(t), greater<int>());
    Rep(i, 30) G[i].clear();
    Rep(i, p) {
      int from, to, len;
      scanf("%d %d %d", &from, &to, &len);
      from--;
      to--;
      G[from].pb(edge(to, len));
      G[to].pb(edge(from, len));
    }
    queue<state> Q;
    state s;
    s.c = a;
    s.prec = -1;
    s.road.clear();
    Q.push(s);
    ans = INF;
    while (!Q.empty()) {
      state ns = Q.front();
      Q.pop();
      // dbg(ns.c);
      if (ns.road.size() <= n && ns.c != b) {
        // dbg(ns.c);
        For(i, 0, G[ns.c].size()) {
          if (ns.prec == G[ns.c][i].to)
            continue;
          // dbg(ns.c);
          state nss;
          nss.c = G[ns.c][i].to;
          nss.prec = ns.c;
          // dbg(ns.road.size());
          // dbg(G[ns.c][i].len);
          nss.road = ns.road;
          nss.road.pb(G[ns.c][i].len);
          Q.push(nss);
        }
      } else if (ns.road.size() <= n && ns.c == b) {
        // dbg(ns.c);
        // dbg(ns.road.size());
        sort(all(ns.road), greater<int>());
        double res = 0;

        For(i, 0, ns.road.size()) { res += (double)ns.road[i] / t[i]; }
        ans = min(res, ans);
      }
    }
    if (ans == INF)
      printf("impossible\\n");
    else
      printf("%f\\n", ans);
  }
  return 0;
}