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

#include <bits/stdc++.h>
using namespace std;

using Weight = double;
struct Edge {
  int to;
  Weight w;
  Edge(int t, Weight ww) : to(t), w(ww){};
};
int n;
int vertex(int pre, int x, int v) { return pre + x * n + v * n * n; }

using Edges = vector<Edge>;
using Graph = vector<Edges>;

const Weight INF = 1e15;
vector<Weight> dijkstra(int s, Graph &g) {
  vector<Weight> res(g.size(), INF);
  vector<int> used(g.size(), false);
  priority_queue<pair<Weight, int>, vector<pair<Weight, int>>, greater<>> que;
  que.push(make_pair(Weight(0), s));
  while (!que.empty()) {
    auto ret = que.top();
    que.pop();
    int v = ret.second;
    if (used[v])
      continue;
    used[v] = true;
    res[v] = ret.first;
    for (int i = 0; i < g[v].size(); i++) {
      que.push(make_pair(res[v] + g[v][i].w, g[v][i].to));
    }
  }
  return res;
}

void solve(int s, int g, Graph &graph) {
  vector<Weight> d = dijkstra(s, graph);
  Weight res = INF;
  for (int i = 0; i < n; i++) {
    res = min(res, d[vertex(i, g, 1)]);
  }
  if (res >= INF / 2) {
    cout << "unreachable" << endl;
  } else {
    cout << fixed << setprecision(10);
    cout << res << endl;
  }
  return;
}

int main() {
  int m;
  while (cin >> n >> m, n) {
    Graph graph(n * n * 32);
    int s, g;
    cin >> s >> g;
    s--, g--;
    for (int i = 0; i < m; i++) {
      int x, y, c;
      Weight d;
      cin >> x >> y >> d >> c;
      x--, y--;
      for (int i = 0; i <= min(31, c + 1); i++) {
        for (int j = -1; j <= 1; j++) {
          if (0 < i + j && i + j <= c) {
            for (int k = 0; k < n; k++) {
              if (k != y && (i != 0 || x == s))
                graph[vertex(k, x, i)].emplace_back(vertex(x, y, i + j),
                                                    d / (i + j));
              if (k != x && (i != 0 || y == s))
                graph[vertex(k, y, i)].emplace_back(vertex(y, x, i + j),
                                                    d / (i + j));
            }
          }
        }
      }
    }
    solve(vertex(s, s, 0), g, graph);
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using Weight = long double;
struct Edge {
  int from, to;
  Weight w;
  Edge(int f, int t, Weight ww) : from(f), to(t), w(ww){};
};
int n;
int vertex(int pre, int x, int v) { return pre + x * n + v * n * n; }

using Edges = vector<Edge>;
using Graph = vector<Edges>;

const Weight INF = 1e15;
vector<Weight> dijkstra(int s, Graph &g) {
  vector<Weight> res(g.size(), INF);
  vector<int> used(g.size(), false);
  priority_queue<pair<Weight, int>, vector<pair<Weight, int>>, greater<>> que;
  que.push(make_pair(Weight(0), s));
  while (!que.empty()) {
    auto ret = que.top();
    que.pop();
    int v = ret.second;
    if (used[v])
      continue;
    used[v] = true;
    res[v] = ret.first;
    for (int i = 0; i < g[v].size(); i++) {
      que.push(make_pair(res[v] + g[v][i].w, g[v][i].to));
    }
  }
  return res;
}

void solve(int s, int g, Graph &graph) {
  vector<Weight> d = dijkstra(s, graph);
  Weight res = INF;
  for (int i = 0; i < n; i++) {
    res = min(res, d[vertex(i, g, 1)]);
  }
  if (res >= INF / 2) {
    cout << "unreachable" << endl;
  } else {
    cout << fixed << setprecision(10);
    cout << res << endl;
  }
  return;
}

int main() {
  int m;
  while (cin >> n >> m, n) {
    Graph graph(n * n * 32);
    int s, g;
    cin >> s >> g;
    s--, g--;
    for (int i = 0; i < m; i++) {
      int x, y, c;
      Weight d;
      cin >> x >> y >> d >> c;
      x--, y--;
      for (int i = 0; i <= min(31, c + 1); i++) {
        for (int j = -1; j <= 1; j++) {
          if (0 < i + j && i + j <= c) {
            for (int k = 0; k < n; k++) {
              if (k != y && (i != 0 || x == s))
                graph[vertex(k, x, i)].emplace_back(
                    vertex(k, x, i), vertex(x, y, i + j), d / (i + j));
              if (k != x && (i != 0 || y == s))
                graph[vertex(k, y, i)].emplace_back(
                    vertex(k, y, i), vertex(y, x, i + j), d / (i + j));
            }
          }
        }
      }
    }
    solve(vertex(s, s, 0), g, graph);
  }
  return 0;
}