#C3017. Minimum Total Cost to Connect Castles

    ID: 46398 Type: Default 1000ms 256MiB

Minimum Total Cost to Connect Castles

Minimum Total Cost to Connect Castles

You are given a set of castles located on a 2D plane. Each castle is represented by its coordinates. The cost to build a road between any two castles is equal to the Euclidean distance between them. The Euclidean distance between two points \((x_1, y_1)\) and \((x_2, y_2)\) is given by:

(x1x2)2+(y1y2)2\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}

Your task is to find a way to connect all the castles so that every castle is reachable from any other castle, while minimizing the total cost of the roads constructed. This is equivalent to finding the Minimum Spanning Tree (MST) of the graph formed by the castles, where each edge weight is the Euclidean distance between the two castles.

Input Format: The first line contains an integer N denoting the number of castles. Each of the next N lines contains two integers representing the x and y coordinates of a castle.

Output Format: Print a single floating‑point number that represents the minimum total cost to connect all castles. The answer should be output with six decimal places.

inputFormat

The input is read from standard input (stdin). The first line contains an integer N (1 ≤ N ≤ 10^5) representing the number of castles. Each of the following N lines contains two integers separated by a space, representing the x and y coordinates of a castle.

outputFormat

The output is written to standard output (stdout) as a single floating‑point number with six decimal places, representing the minimum total cost to connect all castles.## sample

4
0 0
0 1
1 0
1 1
3.000000