Problem p01874 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define _rep(i, n) _repl(i, 0, n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

using P = pair<int, int>;

long dist(P &a, P &b) {
  long x = a.fi - b.fi;
  long y = a.se - b.se;
  return x * x + y * y;
}

double dist(P &a, P &b, P &c) {
  long ipa = (b.fi - a.fi) * (c.fi - a.fi) + (b.se - a.se) * (c.se - a.se);
  long ipb = (a.fi - b.fi) * (c.fi - b.fi) + (a.se - b.se) * (c.se - b.se);
  if (ipa <= 0 || ipb <= 0)
    return min(dist(a, c), dist(b, c));
  else {
    long outacab =
        (c.fi - a.fi) * (b.se - a.se) - (c.se - a.se) * (b.fi - a.fi);
    return (double)outacab * outacab / dist(a, b);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> p(n + 1);
  cin >> p[0].fi >> p[0].se;
  rep(i, 1, n + 1) cin >> p[i].fi >> p[i].se;

  n++;

  vector<bool> used(n, false);
  used[0] = true;
  vector<double> dd(n); // dist^2
  rep(i, n) dd[i] = dist(p[0], p[i]);
  dd[0] = INF;

  double ans = 0;
  rep(_, m) {
    int cand = 0;
    rep(j, 1, n) if (!used[j] && dd[cand] > dd[j]) cand = j;

    double add = INF;
    int from = 0;
    rep(j, n) if (used[j] && dist(p[cand], p[j]) < add) from = j,
                                                        add =
                                                            dist(p[cand], p[j]);

    ans += sqrt(add); // dbg(add,from);
    used[cand] = true;

    rep(j, n) if (!used[j]) dd[j] =
        min<double>(dd[j], dist(p[cand], p[from], p[j]));
  }

  printf("%.10f\\n", ans);

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define _rep(i, n) _repl(i, 0, n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

using P = pair<int, int>;

int dist(P &a, P &b) {
  int x = a.fi - b.fi;
  int y = a.se - b.se;
  return x * x + y * y;
}

int dist(P &a, P &b, P &c) {
  int ipa = (b.fi - a.fi) * (c.fi - a.fi) + (b.se - a.se) * (c.se - a.se);
  int ipb = (a.fi - b.fi) * (c.fi - b.fi) + (a.se - b.se) * (c.se - b.se);
  if (ipa <= 0 || ipb <= 0)
    return min(dist(a, c), dist(b, c));
  else {
    long outacab =
        (c.fi - a.fi) * (b.se - a.se) - (c.se - a.se) * (b.fi - a.fi);
    return outacab * outacab / dist(a, b);
  }
}

int main() {
  int n, m;
  P s;
  cin >> n >> m;
  vector<P> p(n);
  cin >> p[0].fi >> p[0].se;
  rep(i, 1, n + 1) cin >> p[i].fi >> p[i].se;

  n++;

  vector<bool> used(n, false);
  used[0] = true;
  vector<int> dd(n); // dist^2
  rep(i, n) dd[i] = dist(p[0], p[i]);
  dd[0] = INF;

  double ans = 0;
  rep(_, m) {
    int cand = 0;
    rep(j, 1, n) if (!used[j] && dd[cand] > dd[j]) cand = j;
    double add = INF;
    int from = 0;
    rep(j, n) if (used[j] && dist(p[cand], p[j]) < add) from = j,
                                                        add =
                                                            dist(p[cand], p[j]);

    ans += sqrt(add); // dbg(add,from);
    used[cand] = true;

    rep(j, n) if (!used[j]) dd[j] = min(dd[j], dist(p[cand], p[from], p[j]));
  }

  printf("%.10f\\n", ans);

  return 0;
}