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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#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 <utility>
#include <vector>
using namespace std;

// common

typedef int i32;
typedef long long i64, ll;
typedef long double ld;

#define BR "\\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, l, r) for (int i = (int)l; i < (int)(r); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

// config
// #define NDEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp, act)
#define STOP(e)
#else
#define DUMP(x) cerr << #x << " = " << (x)
#define DUMPLN(x) DUMP(x) << endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x) << endl
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl
#define CHECK(exp, act)                                                        \\
  if (exp != act) {                                                            \\
    DUMPLN(exp);                                                               \\
    DEBUGLN(act);                                                              \\
  }
#define STOP(e)                                                                \\
  CHECK(e, true);                                                              \\
  if (!(e))                                                                    \\
    exit(1);
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}

template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

string BITtoString(int bit) {
  stringstream ss;
  while (bit != 0) {
    ss << (bit % 2);
    bit /= 2;
  }
  string res = ss.str();
  reverse(ALL(res));
  return res;
}

template <typename T, typename V> inline T pmod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}
#define nextInt(n) scanf("%d", &n)
#define defInt(n)                                                              \\
  int n;                                                                       \\
  nextInt(n)
#define nextLong(n) scanf("%lld", &n)
#define defLong(n)                                                             \\
  ll n;                                                                        \\
  nextLong(n)
#define nextDouble(n) scanf("%lf", &n)

namespace ShortestPath {
typedef double Cost;
const Cost CINF = 1e17;
typedef vector<vector<Cost>> Mat;
struct Edge {
  int to;
  Cost cost;
  Edge(int to, Cost cost) : to(to), cost(cost){};
};

typedef vector<vector<Edge>> Graph;

struct Task {
  int pos;
  Cost cost;
  Task(int pos, Cost cost) : pos(pos), cost(cost){};
  bool operator>(const Task &r) const { return cost > r.cost; }
};

inline vector<Cost> dijkstra(const Graph &g, const int s, vector<int> &prev) {
  const int V = g.size();
  vector<Cost> d(V, CINF);
  d[s] = 0;

  priority_queue<Task, vector<Task>, greater<Task>> que;
  que.push(Task(s, 0));
  vector<bool> passed(V);
  while (!que.empty()) {
    Task task = que.top();
    que.pop();
    // if(prev[task.pos]!=-2)continue;
    if (passed[task.pos])
      continue;
    // if(d[task.pos]<task.cost)continue;
    passed[task.pos] = true;
    EACH(e, g[task.pos]) {
      if (d[e->to] > d[task.pos] + e->cost) {
        d[e->to] = d[task.pos] + e->cost;
        que.push(Task(e->to, d[e->to]));
      }
    }
  }
  return d;
}
vector<Cost> dijkstra(const Graph &g, const int s) {
  vector<int> prev(g.size());
  return dijkstra(g, s, prev);
}
} // namespace ShortestPath
using namespace ShortestPath;

class Main {
public:
  int enc(int pos, int prev, int V) { return pos * 35 * N + prev * 35 + V; }
  int N, M;

  void run() {
    // ifstream cin("D2");
    //		ofstream cout( "D2.out" );
    cout << fixed << setprecision(15);

    while (true) {
      // cin >> N >> M;
      scanf("%d%d", &N, &M);
      if (N == 0)
        break;
      int s, g;
      // cin >> s >> g;
      scanf("%d%d", &s, &g);
      s--;
      g--;

      Graph G(N * N * 35);
      REP(i, M) {
        int x, y;
        double d;
        int c;
        // cin >> x >> y >> d >> c;
        scanf("%d%d%lf%d", &x, &y, &d, &c);
        x--;
        y--;
        for (int v = 0; v <= 35; v++)
          for (int nv = v - 1; nv <= v + 1; nv++) {
            if (!IN(1, nv, c + 1))
              continue;
            REP(prev, N)
            if (prev != y) G[enc(x, prev, v)].push_back(
                Edge(enc(y, x, nv), d / nv));
            REP(prev, N)
            if (prev != x) G[enc(y, prev, v)].push_back(
                Edge(enc(x, y, nv), d / nv));
          }
      }
      vector<Cost> res = dijkstra(G, enc(s, s, 0));
      Cost mv = CINF;
      REP(prev, N) mv = min(mv, res[enc(g, prev, 1)]);
      if (mv >= CINF)
        cout << "unreachable" << endl;
      else
        cout << mv << endl;
    }
  }
};
int main() {
  cout << fixed << setprecision(15);
  ios::sync_with_stdio(false);
  Main().run();
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#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 <utility>
#include <vector>
using namespace std;

// common

typedef int i32;
typedef long long i64, ll;
typedef long double ld;

#define BR "\\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, l, r) for (int i = (int)l; i < (int)(r); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

// config
// #define NDEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp, act)
#define STOP(e)
#else
#define DUMP(x) cerr << #x << " = " << (x)
#define DUMPLN(x) DUMP(x) << endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x) << endl
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl
#define CHECK(exp, act)                                                        \\
  if (exp != act) {                                                            \\
    DUMPLN(exp);                                                               \\
    DEBUGLN(act);                                                              \\
  }
#define STOP(e)                                                                \\
  CHECK(e, true);                                                              \\
  if (!(e))                                                                    \\
    exit(1);
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}

template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

string BITtoString(int bit) {
  stringstream ss;
  while (bit != 0) {
    ss << (bit % 2);
    bit /= 2;
  }
  string res = ss.str();
  reverse(ALL(res));
  return res;
}

template <typename T, typename V> inline T pmod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}
#define nextInt(n) scanf("%d", &n)
#define defInt(n)                                                              \\
  int n;                                                                       \\
  nextInt(n)
#define nextLong(n) scanf("%lld", &n)
#define defLong(n)                                                             \\
  ll n;                                                                        \\
  nextLong(n)
#define nextDouble(n) scanf("%lf", &n)

namespace ShortestPath {
typedef double Cost;
const Cost CINF = 1e17;
typedef vector<vector<Cost>> Mat;
struct Edge {
  int id;
  int from, to;
  Cost cost;
  Edge(int id, int from, int to, Cost cost)
      : id(id), from(from), to(to), cost(cost){};
};
ostream &operator<<(ostream &os, const Edge &e) {
  os << "(" << e.from << "->" << e.to << ")";
  return os;
}
typedef vector<vector<Edge>> Graph;

struct Task {
  int prev, pos;
  Cost cost;
  Task(int prev, int pos, Cost cost) : prev(prev), pos(pos), cost(cost){};
  bool operator>(const Task &r) const { return cost > r.cost; }
};

inline vector<Cost> dijkstra(const Graph &g, const int s, vector<int> &prev) {
  const int V = g.size();
  vector<Cost> d(V, CINF);
  d[s] = 0;

  priority_queue<Task, vector<Task>, greater<Task>> que;
  que.push(Task(-1, s, 0));
  vector<bool> passed(V);
  while (!que.empty()) {
    Task task = que.top();
    que.pop();
    // if(prev[task.pos]!=-2)continue;
    if (passed[task.pos])
      continue;
    // if(d[task.pos]<task.cost)continue;
    passed[task.pos] = true;
    EACH(e, g[task.pos]) {
      if (d[e->to] > d[e->from] + e->cost) {
        d[e->to] = d[e->from] + e->cost;
        que.push(Task(e->from, e->to, d[e->to]));
      }
    }
  }
  return d;
}
vector<Cost> dijkstra(const Graph &g, const int s) {
  vector<int> prev(g.size());
  return dijkstra(g, s, prev);
}
} // namespace ShortestPath
using namespace ShortestPath;

class Main {
public:
  int enc(int pos, int prev, int V) { return pos * 35 * N + prev * 35 + V; }
  int N, M;

  void run() {
    // ifstream cin("D2");
    //		ofstream cout( "D2.out" );
    cout << fixed << setprecision(15);

    while (true) {
      // cin >> N >> M;
      scanf("%d%d", &N, &M);
      if (N == 0)
        break;
      int s, g;
      // cin >> s >> g;
      scanf("%d%d", &s, &g);
      s--;
      g--;

      Graph G(N * N * 35);
      REP(i, M) {
        int x, y;
        double d;
        int c;
        // cin >> x >> y >> d >> c;
        scanf("%d%d%lf%d", &x, &y, &d, &c);
        x--;
        y--;
        for (int v = 0; v <= 35; v++)
          for (int nv = v - 1; nv <= v + 1; nv++) {
            if (!IN(1, nv, c + 1))
              continue;
            REP(prev, N)
            if (prev != y) G[enc(x, prev, v)].push_back(
                Edge(i, enc(x, prev, v), enc(y, x, nv), d / nv));
            REP(prev, N)
            if (prev != x) G[enc(y, prev, v)].push_back(
                Edge(i, enc(y, prev, v), enc(x, y, nv), d / nv));
          }
      }
      vector<Cost> res = dijkstra(G, enc(s, s, 0));
      Cost mv = CINF;
      REP(prev, N) mv = min(mv, res[enc(g, prev, 1)]);
      if (mv >= CINF)
        cout << "unreachable" << endl;
      else
        cout << mv << endl;
    }
  }
};
int main() {
  cout << fixed << setprecision(15);
  ios::sync_with_stdio(false);
  Main().run();
  return 0;
}