Language: C++

#include<stdio.h>
using namespace std;

typedef double R;

template <class T>
class _Point{
public:
	T x;
	T y;
	_Point(){}
	_Point(T _x, T _y) :x(_x), y(_y){}
	_Point(const _Point & v) :x(v.x), y(v.y){}

	const _Point operator + (const _Point & v)
	{
		return _Point(x + v.x, y + v.y);
	}
	const _Point operator - (const _Point & v)
	{
		return _Point(x - v.x, y - v.y);
	}
	T abs() const {
		return sqrt(x*x + y*y);
	}
	_Point unit() const{
		T len = abs();
		return _Point(x / len, y / len);
	}
	T operator*(const _Point &v) const
	{
		return x * v.x + y * v.y;
	}
	const _Point operator*(const T &len) const
	{
		return _Point(len * x, len * y);
	}
	bool operator==(const _Point &p) const
	{
		return (x == p.x) && (y == p.y);
	}
	R distance(const _Point &t) const
	{
		return sqrt((x - t.x)*(x - t.x) + (y - t.y)*(y - t.y));
	}
	bool operator<(const _Point &p) const
	{
		return 0;
	}
};

typedef _Point<R> Point;
typedef Point Vector;

class SegmentLine
{
public:
	Point start, end;
	R distance() const
	{
		return start.distance(end);
	}
	SegmentLine(const Point & _s, const Point & _e) :start(_s), end(_e){}

	//????????¨???????????¢
	R distance(const Point t) const
	{
		Vector dir = end;
		dir = dir - start;
		if (dir.abs() < DBL_EPSILON * 16)
		{
			return t.distance(start);
		}

		Vector e = dir.unit();
		Vector v = t;
		v = v - start;
		R inner = v * e;
		if (inner <= 0)
		{
			return start.distance(t);
		}
		else if (inner > distance())
		{
			return end.distance(t);
		}
		else
		{
			double d = v.abs();
			return sqrt(d*d - inner*inner);
		}
	}
};

typedef pair<int, Point> indexedPoint;
class Kyoten
{
	list<indexedPoint> _points;
	list<SegmentLine> _lines;
public:
	R totallength()
	{
		R length = 0;
		for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
		{
			length += it->distance();
		}
		return length;
	}
	//p ??¨????????¢?????????
	R distance(const Point p)
	{
		R mindist = -1;
		for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
		{
			R dist = it->distance(p);
			if (mindist < 0 || dist < mindist)
			{
				mindist = dist;
			}
		}
		if (mindist < 0)
		{
			for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
			{
				R dist = it->second.distance(p);
				if (mindist < 0 || dist < mindist)
				{
					mindist = dist;
				}
			}
		}
		return mindist;
	}
	void add(int index,const Point & p)
	{
		R mindistance = -1;
		Point near;
		for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
		{
			Point & itp = it->second;
			if (itp == p){
				return;
			}
			R distance = itp.distance(p);
			if (mindistance < 0 || distance < mindistance)
			{
				mindistance = distance;
				near = itp;
			}
		}
		_points.push_back(indexedPoint(index, p));
		_points.sort();
		if (mindistance >= 0)
		{
			_lines.push_back(SegmentLine(near, p));
		}
	}
};

int main(){
	std::istream & c_in = cin;

	int N, M, X, Y;
	c_in >> N >> M >> X >> Y;

	Kyoten kyoten;
	kyoten.add(0,Point(X, Y));

	list<indexedPoint> P;
	P.resize(N);
	for (int i = 0; i < N; ++i)
	{
		Point p;
		c_in >> p.x >> p.y;
		P.push_back(indexedPoint(i + 1, p));
	}

	int esa = 0;
	while (esa < M)
	{
		R dist;
		list<indexedPoint>::iterator nearby = P.end();
		for (list<indexedPoint>::iterator it = P.begin(); it != P.end(); ++it)
		{
			R d = kyoten.distance(it->second);
			if ((nearby == P.end()) || d < dist)
			{
				dist = d;
				nearby = it;
			}
		}
		kyoten.add(nearby->first,nearby->second);
		P.erase(nearby);
		esa++;
	}
	//
	cout << fixed << setprecision(10) << kyoten.totallength() << endl;
}

Compiler output:
a.cc: In member function 'R SegmentLine::distance(Point) const':
a.cc:70:33: error: 'DBL_EPSILON' was not declared in this scope
   70 |                 if (dir.abs() < DBL_EPSILON * 16)
      |                                 ^~~~~~~~~~~
a.cc:90:32: error: 'sqrt' was not declared in this scope
   90 |                         return sqrt(d*d - inner*inner);
      |                                ^~~~
a.cc: At global scope:
a.cc:95:9: error: 'pair' does not name a type
   95 | typedef pair<int, Point> indexedPoint;
      |         ^~~~
a.cc:98:9: error: 'list' does not name a type
   98 |         list<indexedPoint> _points;
      |         ^~~~
a.cc:99:9: error: 'list' does not name a type
   99 |         list<SegmentLine> _lines;
      |         ^~~~
a.cc: In member function 'R Kyoten::totallength()':
a.cc:104:22: error: 'list' was not declared in this scope
  104 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                      ^~~~
a.cc:2:1: note: 'std::list' is defined in header '<list>'; this is probably fixable by adding '#include <list>'
    1 | #include<stdio.h>
  +++ |+#include <list>
    2 | using namespace std;
a.cc:104:38: error: expected primary-expression before '>' token
  104 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                                      ^
a.cc:104:41: error: '::iterator' has not been declared
  104 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                                         ^~~~~~~~
a.cc:104:71: error: 'it' was not declared in this scope; did you mean 'int'?
  104 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                                                                       ^~
      |                                                                       int
a.cc:104:77: error: '_lines' was not declared in this scope
  104 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                                                                             ^~~~~~
a.cc: In member function 'R Kyoten::distance(Point)':
a.cc:114:22: error: 'list' was not declared in this scope
  114 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                      ^~~~
a.cc:114:22: note: 'std::list' is defined in header '<list>'; this is probably fixable by adding '#include <list>'
a.cc:114:38: error: expected primary-expression before '>' token
  114 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                                      ^
a.cc:114:41: error: '::iterator' has not been declared
  114 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                                         ^~~~~~~~
a.cc:114:71: error: 'it' was not declared in this scope; did you mean 'int'?
  114 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                                                                       ^~
      |                                                                       int
a.cc:114:77: error: '_lines' was not declared in this scope
  114 |                 for (list<SegmentLine>::iterator it = _lines.begin(); it != _lines.end(); ++it)
      |                                                                             ^~~~~~
a.cc:124:30: error: 'list' was not declared in this scope
  124 |                         for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                              ^~~~
a.cc:124:30: note: 'std::list' is defined in header '<list>'; this is probably fixable by adding '#include <list>'
a.cc:124:35: error: 'indexedPoint' was not declared in this scope
  124 |                         for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                                   ^~~~~~~~~~~~
a.cc:124:50: error: '::iterator' has not been declared
  124 |                         for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                                                  ^~~~~~~~
a.cc:124:81: error: 'it' was not declared in this scope; did you mean 'int'?
  124 |                         for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                                                                                 ^~
      |                                                                                 int
a.cc:124:87: error: '_points' was not declared in this scope; did you mean '_Point'?
  124 |                         for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                                                                                       ^~~~~~~
      |                                                                                       _Point
a.cc: In member function 'void Kyoten::add(int, const Point&)':
a.cc:139:22: error: 'list' was not declared in this scope
  139 |                 for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                      ^~~~
a.cc:139:22: note: 'std::list' is defined in header '<list>'; this is probably fixable by adding '#include <list>'
a.cc:139:27: error: 'indexedPoint' was not declared in this scope
  139 |                 for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                           ^~~~~~~~~~~~
a.cc:139:42: error: '::iterator' has not been declared
  139 |                 for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                                          ^~~~~~~~
a.cc:139:73: error: 'it' was not declared in this scope; did you mean 'int'?
  139 |                 for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                                                                         ^~
      |                                                                         int
a.cc:139:79: error: '_points' was not declared in this scope; did you mean '_Point'?
  139 |                 for (list<indexedPoint>::iterator it = _points.begin(); it != _points.end(); ++it)
      |                                                                               ^~~~~~~
      |                                                                               _Point
a.cc:152:17: error: '_points' was not declared in this scope; did you mean '_Point'?
  152 |                 _points.push_back(indexedPoint(index, p));
      |                 ^~~~~~~
      |                 _Point
a.cc:152:35: error: 'indexedPoint' was not declared in this scope
  152 |                 _points.push_back(indexedPoint(index, p));
      |                                   ^~~~~~~~~~~~
a.cc:156:25: error: '_lines' was not declared in this scope
  156 |                         _lines.push_back(SegmentLine(near, p));
      |                         ^~~~~~
a.cc: In function 'int main()':
a.cc:162:14: error: 'istream' is not a member of 'std'
  162 |         std::istream & c_in = cin;
      |              ^~~~~~~
a.cc:2:1: note: 'std::istream' is defined in header '<istream>'; this is probably fixable by adding '#include <istream>'
    1 | #include<stdio.h>
  +++ |+#include <istream>
    2 | using namespace std;
a.cc:162:24: error: 'c_in' was not declared in this scope
  162 |         std::istream & c_in = cin;
      |                        ^~~~
a.cc:162:31: error: 'cin' was not declared in this scope
  162 |         std::istream & c_in = cin;
      |                               ^~~
a.cc:2:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    1 | #include<stdio.h>
  +++ |+#include <iostream>
    2 | using namespace std;
a.cc:170:9: error: 'list' was not declared in this scope
  170 |         list<indexedPoint> P;
      |         ^~~~
a.cc:170:9: note: 'std::list' is defined in header '<list>'; this is probably fixable by adding '#include <list>'
a.cc:170:14: error: 'indexedPoint' was not declared in this scope
  170 |         list<indexedPoint> P;
      |              ^~~~~~~~~~~~
a.cc:170:28: error: 'P' was not declared in this scope
  170 |         list<indexedPoint> P;
      |                            ^
a.cc:183:37: error: '::iterator' has not been declared
  183 |                 list<indexedPoint>::iterator nearby = P.end();
      |                                     ^~~~~~~~
a.cc:184:42: error: '::iterator' has not been declared
  184 |                 for (list<indexedPoint>::iterator it = P.begin(); it != P.end(); ++it)
      |                                          ^~~~~~~~
a.cc:184:67: error: 'it' was not declared in this scope; did you mean 'int'?
  184 |                 for (list<indexedPoint>::iterator it = P.begin(); it != P.end(); ++it)
      |                                                                   ^~
      |                                                                   int
a.cc:187:30: error: 'nearby' was not declared in this scope
  187 |                         if ((nearby == P.end()) || d < dist)
      |                              ^~~~~~
a.cc:193:28: error: 'nearby' was not declared in this scope
  193 |                 kyoten.add(nearby->first,nearby->second);
      |                            ^~~~~~
a.cc:198:9: error: 'cout' was not declared in this scope
  198 |         cout << fixed << setprecision(10) << kyoten.totallength() << endl;
      |         ^~~~
a.cc:198:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:198:17: error: 'fixed' was not declared in this scope
  198 |         cout << fixed << setprecision(10) << kyoten.totallength() << endl;
      |                 ^~~~~
a.cc:2:1: note: 'std::fixed' is defined in header '<ios>'; th