Problem 022

Problem

Description

Sim City 2000

Recalling your youth, you remember spending long hours playing Sim City. In this game you had the priviledge and honor of building a city to your exact specifications and desires. You could build airports, sports stadiums, schools, industrial factories, residential homes and more. It was your job to keep the citizens of your metropolis happy and prosperous.

Unfortunately, to pay for all of these construction projects, you had to tax your people. Even in the virtual world, people hate taxes and want you to spend their hard-earned income wisely. In particular, they would demand that you build roads to connect every building, but in the most cost efficient way.

Now that you have had some training in graph algorithms, you can easily tackle the problem of connecting every building in your city to every other building using the minimum amount of road. Given the coordinates of the buildings in a city, you are to connect them such that all buildings are reachable by road, while minimizing the amount of pavement you must lay down to make the connections.

Input

The input is a series of building locations specified by sets of point locations. The first line of a set denotes the number of points N, followed by N pair of points X Y. The end of the input is denoted when N = 0. N will never be larger than 100. Here is an sample input:

3
1.0 1.0
2.0 2.0
2.0 4.0
0

Output

For each set of building locations, output the minimum total amount of road that must be constructed to connect all the buildings to two decimal places. Here is an sample output:

3.41

Note

This is based on "10.5.1 Freckles" in "Programming Challenges" by Skiena and Revilla.

Solution

Approach

Solve this problem by forming the minimum spanning tree and keeping track of the edge costs (i.e. amount of road).

Input/Output

Source Code