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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}

ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

ll n, m, p, a, b;
double t[10];

struct edge {
  ll to;
  double cost;
};

struct PP {
  double dis;
  ll pos;
  ll num;

  PP(double _dis, ll _pos, ll _num) : dis(_dis), pos(_pos), num(_num) {}

  bool operator<(const PP &p) const { return dis < p.dis - EPS; }
  bool operator>(const PP &p) const { return dis - EPS > p.dis; }
  bool operator<=(const PP &p) const { return dis < p.dis + EPS; }
  bool operator>=(const PP &p) const { return dis + EPS > p.dis; }
};

double d[100][1 << 9];
vector<edge> G[100]; // 各頂点からの辺
priority_queue<PP, vector<PP>, greater<PP>> que;

void dijkstra(ll start) {
  rep(i, 100) rep(j, (1 << 9)) d[i][j] = INF;
  d[start][0] = 0;
  que.push(PP(0, start, 0));

  while (!que.empty()) {
    PP p = que.top();
    que.pop();
    ll v = p.pos;
    ll nn = p.num;
    if (d[v][nn] < p.dis)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      rep(j, n) {
        if ((1 << j) & nn)
          continue;
        double ncost = e.cost / t[j];
        ll nnum = nn | (1 << j);
        if (d[e.to][nnum] > d[v][nn] + ncost) {
          d[e.to][nnum] = d[v][nn] + ncost;
          que.push(PP(d[e.to][nnum], e.to, nnum));
        }
      }
    }
  }
}

void clear() {
  while (!que.empty())
    que.pop();
  rep(i, 100) G[i].clear();
}

int main() {
  while (cin >> n >> m >> p >> a >> b && n + m + p + a + b) {
    clear();
    rep(i, n) cin >> t[i];
    sort(t, t + n);
    reverse(t, t + n);
    rep(i, p) {
      ll x, y;
      double z;
      cin >> x >> y >> z;
      G[x].push_back({y, z});
      G[y].push_back({x, z});
    }
    dijkstra(a);
    double mincost = INF;
    rep(i, (1 << n)) {
      // cout << i << " " << dk.d[b][i] << endl;
      mincost = min(mincost, d[b][i]);
    }
    if (mincost == INF)
      cout << "Impossible" << endl;
    else
      printf("%.10lf\\n", mincost);
  }
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}

ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

ll n, m, p, a, b;
double t[10];

struct edge {
  ll to;
  double cost;
};

struct PP {
  double dis;
  ll pos;
  ll num;

  PP(double _dis, ll _pos, ll _num) : dis(_dis), pos(_pos), num(_num) {}

  bool operator<(const PP &p) const { return dis < p.dis - EPS; }
  bool operator>(const PP &p) const { return dis - EPS > p.dis; }
  bool operator<=(const PP &p) const { return dis < p.dis + EPS; }
  bool operator>=(const PP &p) const { return dis + EPS > p.dis; }
};

double d[100][1 << 9];
vector<edge> G[100]; // 各頂点からの辺
priority_queue<PP, vector<PP>, greater<PP>> que;

void dijkstra(ll start) {
  rep(i, 100) rep(j, (1 << 9)) d[i][j] = INF;
  d[start][0] = 0;
  que.push(PP(0, start, 0));

  while (!que.empty()) {
    PP p = que.top();
    que.pop();
    ll v = p.pos;
    ll nn = p.num;
    if (d[v][nn] < p.dis - EPS)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      rep(j, n) {
        if ((1 << j) & nn)
          continue;
        double ncost = e.cost / t[j];
        ll nnum = nn | (1 << j);
        if (d[e.to][nnum] + EPS > d[v][nn] + ncost) {
          d[e.to][nnum] = d[v][nn] + ncost;
          que.push(PP(d[e.to][nnum], e.to, nnum));
        }
      }
    }
  }
}

void clear() {
  while (!que.empty())
    que.pop();
  rep(i, 100) G[i].clear();
}

int main() {
  while (cin >> n >> m >> p >> a >> b && n + m + p + a + b) {
    clear();
    rep(i, n) cin >> t[i];
    sort(t, t + n);
    reverse(t, t + n);
    rep(i, p) {
      ll x, y;
      double z;
      cin >> x >> y >> z;
      G[x].push_back({y, z});
      G[y].push_back({x, z});
    }
    dijkstra(a);
    double mincost = INF;
    rep(i, (1 << n)) {
      // cout << i << " " << dk.d[b][i] << endl;
      mincost = min(mincost, d[b][i]);
    }
    if (mincost == INF)
      cout << "Impossible" << endl;
    else
      printf("%.10lf\\n", mincost);
  }
}