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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int MOD = 1000000007;
const int INF = MOD + 1;
const double EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
typedef double Weight;
struct Edge {
  int from, to;
  Weight cost;
  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
  g[from].push_back(Edge{from, to, cost});
}
const double PI = acos(-1.0);
bool eq(double a, double b) { return abs(a - b) < EPS; }
typedef complex<double> Point;
typedef vector<Point> Polygon;

namespace std {
bool operator<(const Point &a, const Point &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std

struct Line {
  Point a, b;
  Line(Point p, Point q) : a(p), b(q){};
  Line(double x1, double y1, double x2, double y2)
      : a(Point(x1, y1)), b(Point(x2, y2)){};
};

struct Circle {
  Point p;
  double r;
  Circle(Point a, double b) : p(a), r(b){};
};

double dot(Point a, Point b) { return real(conj(a) * b); }

double cross(Point a, Point b) { return imag(conj(a) * b); }

int ccw(Point a, Point b, Point c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return 1; // counter cloclwise
  if (cross(b, c) < -EPS)
    return -1; // cloclwise
  if (dot(b, c) < 0)
    return 2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;    // a--c--b on line
}

bool isis_ll(Line l, Line m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS; }

bool isis_ls(Line l, Line s) {
  return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}

bool isis_ss(Line s, Line t) {
  return (ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
          ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);
}

bool isis_lp(Line l, Point p) { return (abs(cross(l.b - p, l.a - p)) < EPS); }

bool isis_sp(Line s, Point p) {
  return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;
}

Point projection(Line l, Point p) {
  Point base = l.b - l.a;
  double r = dot(p - l.a, base) / norm(base);
  return l.a + base * r;
}

Point mirror(Line l, Point p) { return Point(2, 0) * projection(l, p) - p; }

double dist_lp(Line l, Point p) { return abs(p - projection(l, p)); }

double dist_ll(Line l, Line m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); }

double dist_ls(Line l, Line s) {
  if (isis_ls(l, s))
    return 0;
  return min(dist_lp(l, s.a), dist_lp(l, s.b));
}

double dist_sp(Line s, Point p) {
  Point r = projection(s, p);
  if (isis_sp(s, r))
    return abs(r - p);
  return min(abs(s.a - p), abs(s.b - p));
}

double dist_ss(Line s, Line t) {
  if (isis_ss(s, t))
    return 0;
  return min(min(dist_sp(s, t.a), dist_sp(s, t.b)),
             min(dist_sp(t, s.a), dist_sp(t, s.b)));
}

Point is_ll(Line s, Line t) {
  double a = cross(s.b - s.a, t.b - t.a);
  double b = cross(s.b - s.a, s.b - t.a);
  return t.a + b / a * (t.b - t.a);
}

vector<Point> is_cc(Circle c1, Circle c2) {
  vector<Point> res;
  double d = abs(c1.p - c2.p);
  double rc = (d * d + pow(c1.r, 2) - pow(c2.r, 2)) / (2 * d);
  double dfr = pow(c1.r, 2) - rc * rc;
  if (abs(dfr) < EPS)
    dfr = 0;
  if (dfr < 0.0)
    return res;
  double rs = sqrt(dfr);
  Point diff = (c2.p - c1.p) / d;
  res.push_back(c1.p + diff * Point(rc, rs));
  if (dfr != 0.0)
    res.push_back(c1.p + diff * Point(rc, -rs));
  return res;
}

int isis_cc(Circle c1, Circle c2) {
  double d = abs(c1.p - c2.p);
  if (d - c1.r - c2.r < -EPS)
    return -2; // separate
  if (abs(d - c1.r - c2.r) < EPS)
    return -1; // circumscribe
  if (c1.r < c2.r)
    swap(c1, c2);
  if (abs(d - c1.r + c2.r) < EPS)
    return 1; // inscribe
  if (c1.r - d - c2.r > EPS)
    return 2; // involve
  else
    return 0; // intersect
}

bool isis_cl(Circle c, Line l) {
  double d = dist_lp(l, c.p);
  return d - c.r < -EPS;
}

vector<Point> is_cl(Circle c, Line l) {
  vector<Point> res;
  double d = dist_lp(l, c.p);
  if (d > c.r + EPS)
    return res;
  double len = (d > c.r) ? 0 : sqrt(c.r * c.r - d * d);
  Point nor = (l.a - l.b) / abs(l.a - l.b);
  res.push_back(projection(l, c.p) + len * nor);
  if (abs(len) > EPS)
    res.push_back(projection(l, c.p - len * nor));
  return res;
}

vector<Point> is_cs(Circle c, Line s) {
  vector<Point> v = is_cl(c, s), res;
  REP(i, v.size()) {
    if (ccw(s.a, v[i], s.b) == -2)
      res.push_back(v[i]);
  }
  return res;
}

/*--------------------template--------------------*/
void dijkstra(Graph &g, vector<Weight> &d, int s) {
  d.assign(g.size(), INF);
  d[s] = 0;
  typedef pair<Weight, int> P;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));
  while (!que.empty()) {
    Weight dist = que.top().first;
    int v = que.top().second;
    que.pop();
    if (d[v] < dist)
      continue;
    REP(i, g[v].size()) {
      Edge e = g[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int na, nb;
  cin >> na >> nb;
  vector<Point> pa, pb;
  REP(i, na) {
    double x, y;
    cin >> x >> y;
    pa.emplace_back(x, y);
  }
  REP(i, nb) {
    double x, y;
    cin >> x >> y;
    pb.emplace_back(x, y);
  }
  double ans = INF;
  Line la(pa[0], pa[1]);
  Line lb(pb[0], pb[1]);
  Graph g(pa.size());
  REP(i, pa.size()) REP(j, pa.size()) {
    if (i == j)
      continue;
    if (isis_ss(lb, Line(pa[i], pa[j])))
      continue;
    double d = abs(pa[i] - pa[j]);
    add_edge(g, i, j, d);
    add_edge(g, j, i, d);
  }
  vector<Weight> da, db;
  dijkstra(g, da, 0);
  g.clear();
  g.resize(pb.size());
  REP(i, pb.size()) REP(j, pb.size()) {
    if (i == j)
      continue;
    if (isis_ss(la, Line(pb[i], pb[j])))
      continue;
    double d = abs(pb[i] - pb[j]);
    add_edge(g, i, j, d);
    add_edge(g, j, i, d);
  }
  dijkstra(g, db, 0);
  chmin(ans, db[1] + abs(la.a - la.b));
  chmin(ans, da[1] + abs(lb.a - lb.b));
  if (ans >= INF)
    cout << -1 << endl;
  else
    cout << D10 << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int MOD = 1000000007;
const int INF = MOD + 1;
const double EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
typedef double Weight;
struct Edge {
  int from, to;
  Weight cost;
  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
  g[from].push_back(Edge{from, to, cost});
}
const double PI = acos(-1.0);
bool eq(double a, double b) { return abs(a - b) < EPS; }
typedef complex<double> Point;
typedef vector<Point> Polygon;

namespace std {
bool operator<(const Point &a, const Point &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std

struct Line {
  Point a, b;
  Line(Point p, Point q) : a(p), b(q){};
  Line(double x1, double y1, double x2, double y2)
      : a(Point(x1, y1)), b(Point(x2, y2)){};
};

struct Circle {
  Point p;
  double r;
  Circle(Point a, double b) : p(a), r(b){};
};

double dot(Point a, Point b) { return real(conj(a) * b); }

double cross(Point a, Point b) { return imag(conj(a) * b); }

int ccw(Point a, Point b, Point c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return 1; // counter cloclwise
  if (cross(b, c) < -EPS)
    return -1; // cloclwise
  if (dot(b, c) < 0)
    return 2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;    // a--c--b on line
}

bool isis_ll(Line l, Line m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS; }

bool isis_ls(Line l, Line s) {
  return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}

bool isis_ss(Line s, Line t) {
  return (ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
          ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);
}

bool isis_lp(Line l, Point p) { return (abs(cross(l.b - p, l.a - p)) < EPS); }

bool isis_sp(Line s, Point p) {
  return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;
}

Point projection(Line l, Point p) {
  Point base = l.b - l.a;
  double r = dot(p - l.a, base) / norm(base);
  return l.a + base * r;
}

Point mirror(Line l, Point p) { return Point(2, 0) * projection(l, p) - p; }

double dist_lp(Line l, Point p) { return abs(p - projection(l, p)); }

double dist_ll(Line l, Line m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); }

double dist_ls(Line l, Line s) {
  if (isis_ls(l, s))
    return 0;
  return min(dist_lp(l, s.a), dist_lp(l, s.b));
}

double dist_sp(Line s, Point p) {
  Point r = projection(s, p);
  if (isis_sp(s, r))
    return abs(r - p);
  return min(abs(s.a - p), abs(s.b - p));
}

double dist_ss(Line s, Line t) {
  if (isis_ss(s, t))
    return 0;
  return min(min(dist_sp(s, t.a), dist_sp(s, t.b)),
             min(dist_sp(t, s.a), dist_sp(t, s.b)));
}

Point is_ll(Line s, Line t) {
  double a = cross(s.b - s.a, t.b - t.a);
  double b = cross(s.b - s.a, s.b - t.a);
  return t.a + b / a * (t.b - t.a);
}

vector<Point> is_cc(Circle c1, Circle c2) {
  vector<Point> res;
  double d = abs(c1.p - c2.p);
  double rc = (d * d + pow(c1.r, 2) - pow(c2.r, 2)) / (2 * d);
  double dfr = pow(c1.r, 2) - rc * rc;
  if (abs(dfr) < EPS)
    dfr = 0;
  if (dfr < 0.0)
    return res;
  double rs = sqrt(dfr);
  Point diff = (c2.p - c1.p) / d;
  res.push_back(c1.p + diff * Point(rc, rs));
  if (dfr != 0.0)
    res.push_back(c1.p + diff * Point(rc, -rs));
  return res;
}

int isis_cc(Circle c1, Circle c2) {
  double d = abs(c1.p - c2.p);
  if (d - c1.r - c2.r < -EPS)
    return -2; // separate
  if (abs(d - c1.r - c2.r) < EPS)
    return -1; // circumscribe
  if (c1.r < c2.r)
    swap(c1, c2);
  if (abs(d - c1.r + c2.r) < EPS)
    return 1; // inscribe
  if (c1.r - d - c2.r > EPS)
    return 2; // involve
  else
    return 0; // intersect
}

bool isis_cl(Circle c, Line l) {
  double d = dist_lp(l, c.p);
  return d - c.r < -EPS;
}

vector<Point> is_cl(Circle c, Line l) {
  vector<Point> res;
  double d = dist_lp(l, c.p);
  if (d > c.r + EPS)
    return res;
  double len = (d > c.r) ? 0 : sqrt(c.r * c.r - d * d);
  Point nor = (l.a - l.b) / abs(l.a - l.b);
  res.push_back(projection(l, c.p) + len * nor);
  if (abs(len) > EPS)
    res.push_back(projection(l, c.p - len * nor));
  return res;
}

vector<Point> is_cs(Circle c, Line s) {
  vector<Point> v = is_cl(c, s), res;
  REP(i, v.size()) {
    if (ccw(s.a, v[i], s.b) == -2)
      res.push_back(v[i]);
  }
  return res;
}

/*--------------------template--------------------*/
void dijkstra(Graph &g, vector<Weight> &d, int s) {
  d.assign(g.size(), INF);
  d[s] = 0;
  typedef pair<Weight, int> P;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));
  while (!que.empty()) {
    Weight dist = que.top().first;
    int v = que.top().second;
    que.pop();
    if (d[v] < dist)
      continue;
    REP(i, g[v].size()) {
      Edge e = g[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int na, nb;
  cin >> na >> nb;
  vector<Point> pa, pb;
  REP(i, na) {
    double x, y;
    cin >> x >> y;
    pa.emplace_back(x, y);
  }
  REP(i, nb) {
    double x, y;
    cin >> x >> y;
    pb.emplace_back(x, y);
  }
  double ans = INF;
  Line la(pa[0], pa[1]);
  Line lb(pb[0], pb[1]);
  Graph ga(pa.size()), gb(pb.size());
  REP(i, pa.size()) REP(j, pa.size()) {
    if (i == j)
      continue;
    if (isis_ss(lb, Line(pa[i], pa[j])))
      continue;
    double d = abs(pa[i] - pa[j]);
    add_edge(ga, i, j, d);
    add_edge(ga, j, i, d);
  }
  REP(i, pb.size()) REP(j, pb.size()) {
    if (i == j)
      continue;
    if (isis_ss(la, Line(pb[i], pb[j])))
      continue;
    double d = abs(pb[i] - pb[j]);
    add_edge(gb, i, j, d);
    add_edge(gb, j, i, d);
  }
  vector<Weight> da, db;
  dijkstra(ga, da, 0);
  dijkstra(gb, db, 0);
  chmin(ans, db[1] + abs(la.a - la.b));
  chmin(ans, da[1] + abs(lb.a - lb.b));
  if (ans >= INF)
    cout << -1 << endl;
  else
    cout << D10 << ans << endl;
  return 0;
}