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

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P; // Point
typedef pair<P, P> L;      // Line, Segment

const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);

namespace std {
bool operator<(const P &a, const P &b) {
  return fabs(real(a) - real(b)) < EPS ? imag(a) < imag(b) : real(a) < real(b);
}
} // namespace std
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 getDistanceSP(L s, P p) {
  if (dot(s.second - s.first, p - s.first) < -EPS)
    return abs(p - s.first);
  if (dot(s.first - s.second, p - s.second) < -EPS)
    return abs(p - s.second);
  return abs(cross(s.second - s.first, p - s.first) /
             abs(s.second - s.first)); //??´?????¨???????????¢
}

int N, M, X, Y;
P pos[5555];
typedef pair<double, int> PA;
bool used[5555];
double ky[5555];
priority_queue<PA, vector<PA>, greater<PA>> q;

void insert_seg(const L &seg) {
  for (int i = 0; i <= N; i++) {
    if (used[i])
      continue;
    double x = getDistanceSP(seg, pos[i]);
    if (ky[i] < x)
      continue;
    ky[i] = x;
    q.push(PA(x, i));
  }
}

int main() {
  cin >> N >> M >> X >> Y;
  fill(ky, ky + N + 1, 1e300);
  used[0] = true;
  pos[0] = P(X, Y);
  P kp = P(X, Y);
  PA k = PA(1e300, 0);
  for (int i = 1; i <= N; i++) {
    int x, y;
    cin >> x >> y;
    pos[i] = P(x, y);
    k = min(k, PA(abs(pos[i] - kp), i));
  }
  vector<L> seg;
  used[k.second] = true;
  seg.push_back(L(pos[k.second], kp));
  insert_seg(L(pos[k.second], kp));

  int cnt = 1;
  while (!q.empty() && cnt < M) {
    PA p = q.top();
    q.pop();
    if (used[p.second])
      continue;
    cnt++;
    PA kx = PA(1e300, 0);
    P kp = pos[p.second];
    for (int i = 0; i <= N; i++) {
      if (used[i]) {
        //	cout << kx.first << " - " << abs(kp-pos[i]) << " i " << i <<
        //endl;
        kx = min(kx, PA(abs(kp - pos[i]), i));
        ///	cout << kx.first << " " << kx.second << endl;
      }
    }
    used[p.second] = true;
    insert_seg(L(kp, pos[kx.second]));
    seg.push_back(L(kp, pos[kx.second]));
  }

  double res = 0.0;
  // cout << seg.size()<< " "<< cnt  << endl;
  for (int i = 0; i < seg.size(); i++) {
    // cout << seg[i].first << " - " << seg[i].second << endl;
    res += abs(seg[i].first - seg[i].second);
  }
  printf("%.10lf\\n", res);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P; // Point
typedef pair<P, P> L;      // Line, Segment

const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);

namespace std {
bool operator<(const P &a, const P &b) {
  return fabs(real(a) - real(b)) < EPS ? imag(a) < imag(b) : real(a) < real(b);
}
} // namespace std
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 getDistanceSP(L s, P p) {
  if (dot(s.second - s.first, p - s.first) < -EPS)
    return abs(p - s.first);
  if (dot(s.first - s.second, p - s.second) < -EPS)
    return abs(p - s.second);
  return abs(cross(s.second - s.first, p - s.first) /
             abs(s.second - s.first)); //??´?????¨???????????¢
}

int N, M, X, Y;
P pos[5555];
typedef pair<double, int> PA;
bool used[5555];
priority_queue<PA, vector<PA>, greater<PA>> q;

void insert_seg(const L &seg) {
  for (int i = 0; i <= N; i++) {
    if (used[i])
      continue;
    double x = getDistanceSP(seg, pos[i]);
    q.push(PA(x, i));
  }
}

int main() {
  cin >> N >> M >> X >> Y;
  used[0] = true;
  pos[0] = P(X, Y);
  P kp = P(X, Y);
  PA k = PA(1e300, 0);
  for (int i = 1; i <= N; i++) {
    int x, y;
    cin >> x >> y;
    pos[i] = P(x, y);
    k = min(k, PA(abs(pos[i] - kp), i));
  }
  vector<L> seg;
  used[k.second] = true;
  seg.push_back(L(pos[k.second], kp));
  insert_seg(L(pos[k.second], kp));

  int cnt = 1;
  while (!q.empty() && cnt < M) {
    PA p = q.top();
    q.pop();
    if (used[p.second])
      continue;
    cnt++;
    PA kx = PA(1e300, 0);
    P kp = pos[p.second];
    for (int i = 0; i <= N; i++) {
      if (used[i]) {
        //	cout << kx.first << " - " << abs(kp-pos[i]) << " i " << i <<
        //endl;
        kx = min(kx, PA(abs(kp - pos[i]), i));
        ///	cout << kx.first << " " << kx.second << endl;
      }
    }
    used[p.second] = true;
    insert_seg(L(kp, pos[kx.second]));
    seg.push_back(L(kp, pos[kx.second]));
  }

  double res = 0.0;
  // cout << seg.size()<< " "<< cnt  << endl;
  for (int i = 0; i < seg.size(); i++) {
    // cout << seg[i].first << " - " << seg[i].second << endl;
    res += abs(seg[i].first - seg[i].second);
  }
  printf("%.10lf\\n", res);
}