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

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

typedef complex<double> P;
typedef pair<double, pair<int, pair<int, int>>> PLI;

#define EPS 1e-10

double dot(P a, P b) { return a.real() * b.real() + a.imag() * b.imag(); }

double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
/*
double distance( P a, P b, P c ) {
        return abs( cross( b - a, c - a ) ) / abs( b - a );
}*/

double distance_p2p(P a, P b) {
  return sqrtl(powl((a.real() - b.real()), 2) + powl((a.imag() - b.imag()), 2));
}

double distance_ls_p(P a, P b, P c) {
  if (dot(b - a, c - a) < EPS) {
    return abs(c - a);
  }
  if (dot(a - b, c - b) < EPS) {
    return abs(c - b);
  }
  return abs(cross(b - a, c - a)) / abs(b - a);
}

int main() {
  int N, M, X, Y;
  cin >> N >> M >> X >> Y;
  vector<P> points(N + 1);
  points[0] = {(double)X, (double)Y};
  for (size_t i = 1; i <= N; i++) {
    double x, y;
    cin >> x >> y;
    points[i] = {x, y};
  }
  priority_queue<PLI, vector<PLI>, greater<PLI>> que;
  vector<bool> is_group(N + 1);
  vector<double> alllength(N + 1, LDBL_MAX / 6);
  alllength[0] = 0;
  is_group[0] = true;
  int now_group_num = 1;

  for (size_t i = 1; i <= N; i++) {
    double dis = distance_p2p(points[0], points[i]);
    que.push(make_pair(dis, make_pair(i, make_pair(0, 0))));
  }
  double ans = 0.0L;
  while (now_group_num <= M && que.size()) {
    auto now = que.top();
    que.pop();
    int point_id = now.second.first;
    if (is_group[point_id]) {
      continue;
    }
    int point_next_id = 0;
    /*
    double lengefirst = distance_p2p( points[point_id],
    points[now.second.second.first] ); double lengesecond = distance_p2p(
    points[point_id], points[now.second.second.second] );

    if( abs( lengefirst - lengesecond ) < EPS ) {
            point_next_id = min( now.second.second.first,
    now.second.second.second ); } else { if( lengefirst < lengesecond ) {
                    point_next_id = now.second.second.first;
            } else {
                    point_next_id = now.second.second.second;
            }
    }
    */
    double next_length = LDBL_MAX / 6;
    for (size_t i = 0; i <= N; i++) {
      if (is_group[i]) {
        double x_length = distance_p2p(points[point_id], points[i]);
        if (next_length > x_length + EPS) {
          point_next_id = i;
          next_length = x_length;
        }
      }
    }
    // cout << point_id << " " << fixed << setprecision( 20 ) << now.first <<
    // endl;
    ans += distance_p2p(points[point_id], points[point_next_id]);
    is_group[point_id] = true;
    now_group_num++;
    for (size_t i = 0; i <= N; i++) {
      if (!is_group[i]) {
        double len =
            distance_ls_p(points[point_id], points[point_next_id], points[i]);

        // cout << "!" << i << " " << point_id << " " << point_next_id << " " <<
        // fixed << setprecision( 20 ) << distance_ls_p( points[point_id],
        // points[point_next_id], points[i] ) << endl;
        if (len + EPS < alllength[i]) {
          alllength[i] = len;
          que.push(make_pair(
              distance_ls_p(points[point_id], points[point_next_id], points[i]),
              make_pair(i, make_pair(point_id, point_next_id))));
        }
      }
    }
  }
  cout << fixed << setprecision(20) << ans << endl;
}

Error: MLE

Fixed submission:

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

typedef complex<double> P;
typedef pair<double, pair<int, pair<int, int>>> PLI;

#define EPS 1e-10

double dot(P a, P b) { return a.real() * b.real() + a.imag() * b.imag(); }

double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
/*
double distance( P a, P b, P c ) {
        return abs( cross( b - a, c - a ) ) / abs( b - a );
}*/

double distance_p2p(P a, P b) {
  return sqrtl(powl((a.real() - b.real()), 2) + powl((a.imag() - b.imag()), 2));
}

double distance_ls_p(P a, P b, P c) {
  if (dot(b - a, c - a) < EPS) {
    return abs(c - a);
  }
  if (dot(a - b, c - b) < EPS) {
    return abs(c - b);
  }
  return abs(cross(b - a, c - a)) / abs(b - a);
}

int main() {
  int N, M, X, Y;
  cin >> N >> M >> X >> Y;
  vector<P> points(N + 1);
  points[0] = {(double)X, (double)Y};
  for (size_t i = 1; i <= N; i++) {
    double x, y;
    cin >> x >> y;
    points[i] = {x, y};
  }
  priority_queue<PLI, vector<PLI>, greater<PLI>> que;
  vector<bool> is_group(N + 1);
  is_group[0] = true;
  int now_group_num = 1;

  for (size_t i = 1; i <= N; i++) {
    double dis = distance_p2p(points[0], points[i]);
    que.push(make_pair(dis, make_pair(i, make_pair(0, 0))));
  }
  double ans = 0.0L;
  while (now_group_num <= M && que.size()) {
    auto now = que.top();
    que.pop();
    int point_id = now.second.first;
    if (is_group[point_id]) {
      continue;
    }
    int point_next_id = 0;
    /*
    double lengefirst = distance_p2p( points[point_id],
    points[now.second.second.first] ); double lengesecond = distance_p2p(
    points[point_id], points[now.second.second.second] );

    if( abs( lengefirst - lengesecond ) < EPS ) {
            point_next_id = min( now.second.second.first,
    now.second.second.second ); } else { if( lengefirst < lengesecond ) {
                    point_next_id = now.second.second.first;
            } else {
                    point_next_id = now.second.second.second;
            }
    }
    */
    double next_length = LDBL_MAX / 6;
    for (size_t i = 0; i <= N; i++) {
      if (is_group[i]) {
        double x_length = distance_p2p(points[point_id], points[i]);
        if (next_length > x_length + EPS) {
          point_next_id = i;
          next_length = x_length;
        }
      }
    }
    // cout << point_id << " " << fixed << setprecision( 20 ) << now.first <<
    // endl;
    ans += distance_p2p(points[point_id], points[point_next_id]);
    is_group[point_id] = true;
    now_group_num++;
    for (size_t i = 0; i <= N; i++) {
      if (!is_group[i]) {
        // cout << "!" << i << " " << point_id << " " << point_next_id << " " <<
        // fixed << setprecision( 20 ) << distance_ls_p( points[point_id],
        // points[point_next_id], points[i] ) << endl;
        que.push(make_pair(
            distance_ls_p(points[point_id], points[point_next_id], points[i]),
            make_pair(i, make_pair(point_id, point_next_id))));
      }
    }
  }
  cout << fixed << setprecision(20) << ans << endl;
}