Problem:
Building a Space Station

You are a member of the space station engineering team,
and are assigned a task in the construction process of the station.
You are expected to write a computer program to complete the task.

The space station is made up with a number of units, called cells.
All cells are sphere-shaped, but their sizes are not necessarily uniform.
Each cell is fixed at its predetermined position
shortly after the station is successfully put into its orbit.
It is quite strange that two cells may be touching each other,
or even may be overlapping.
In an extreme case, a cell may be totally enclosing another one.
I do not know how such arrangements are possible.

All the cells must be connected,
since crew members should be able to walk
from any cell to any other cell.
They can walk from a cell A to another cell B, if,
(1) A and B are touching each other or overlapping,
(2) A and B are connected by a `corridor',
or (3) there is a cell C such that walking from A to C, and also from B to C
are both possible.
Note that the condition (3) should be interpreted transitively.

You are expected to design a configuration,
namely, which pairs of cells are to be connected with corridors.
There is some freedom in the corridor configuration.
For example, if there are three cells A, B and C,
not touching nor overlapping each other,
at least three plans are possible in order to connect all three cells.
The first is to build corridors A-B and A-C,
the second B-C and B-A, the third C-A and C-B.
The cost of building a corridor is proportional to its length.
Therefore, you should choose a plan with the shortest
total length of the corridors.

You can ignore the width of a corridor.
A corridor is built between points on two cells' surfaces.
It can be made arbitrarily long, but of course the shortest one is chosen.
Even if two corridors A-B and C-D intersect in space, they are
not considered to form a connection path between (for example) A and C.
In other words, you may consider that two corridors never intersect.

Input

The input consists of multiple data sets.
Each data set is given in the following format.

n

x1 y1 z1 r1

x2 y2 z2 r2

...

xn yn zn rn

The first line of a data set contains an integer n,
which is the number of cells.
n is positive, and does not exceed 100.

The following n lines are descriptions of cells.
Four values in a line are x-, y- and z-coordinates of
the center, and radius (called r in the rest of the problem) of
the sphere, in this order.
Each value is given by a decimal fraction, with
3 digits after the decimal point.
Values are separated by a space character.

Each of x, y, z and r is positive
and is less than 100.0.

The end of the input is indicated by a line containing a zero.

Output

For each data set, the shortest total length of the corridors
should be printed, each in a separate line.
The printed values should have 3 digits after the decimal point.
They may not have an error greater than 0.001.

Note that if no corridors are necessary, that is,
if all the cells are connected without corridors,
the shortest total length of the corridors is 0.000.

Sample Input

3
10.000 10.000 50.000 10.000
40.000 10.000 50.000 10.000
40.000 40.000 50.000 10.000
2
30.000 30.000 30.000 20.000
40.000 40.000 40.000 20.000
5
5.729 15.143 3.996 25.837
6.013 14.372 4.818 10.671
80.115 63.292 84.477 15.120
64.095 80.924 70.029 14.881
39.472 85.116 71.369 5.553
0

Output for the Sample Input

20.000
0.000
73.834

Solution in Ruby (judge verdict: Accepted)

class Heap
	require 'forwardable'
	extend Forwardable

	def initialize
		@array = []
		@index = {}
	end

	def add(obj, key)
		@array << [obj, key]
		@index[obj] = @array.size - 1
		lift_up(@array.size - 1)
	end

	def swap(i, j)
		u = @array[i]
		v = @array[j]
		@index[u[0]], @index[v[0]] = j, i
		@array[i], @array[j] = v, u
	end

	def lift_up(i)
		return if i == 0
		j = (i - 1) / 2
		if @array[i][1] < @array[j][1]
			swap(i, j)
			lift_up(j)
		end
	end

	def del_min
		raise "heap is empty!" if empty?
		min = @array[0]
		if size > 1
			swap(0, @array.size-1)
			@array.pop
			lift_down(0)
		else
			@array.pop
		end
		@index.delete(min[0])
		min
	end

	def inc_key(obj, diff)
		i = @index[obj]
		@array[i][1] += diff
		lift_down(i)
	end

	def dec_key(obj, diff)
		i = @index[obj]
		@array[i][1] -= diff
		lift_up(i)
	end

	def update_key(obj, new_key)
		i = @index[obj]
		return unless i
		return if new_key >= @array[i][1]
		@array[i][1] = new_key
		lift_up(i)
	end

	def min_obj
		@array[0][0]
	end

	def min_key
		@array[0][1]
	end

	def lift_down(i)
		j = 2 * i + 1
		k = j + 1
		if k >= size
			return if j >= size
			if @array[i][1] > @array[j][1]
				swap(i, j)
				lift_down(j)
			end
		else
			l = (@array[j][1] < @array[k][1] ? j : k)
			if @array[i][1] > @array[l][1]
				swap(i, l)
				lift_down(l)
			end
		end
	end

	def_delegators :@array, :empty?, :size
end

module Graphical
	require 'set'

	def reachable_from(s, max_depth = nil)
		reached = Set[s]
		queue = [s]
		depth = 0
		loop {
			break if max_depth && depth >= max_depth
			next_queue = []
			until queue.empty?
				u = queue.shift
				each_to_node_of(u){|v|
					next if reached.include?(v)
					reached << v
					next_queue << v
				}
			end
			break if next_queue.empty?
			queue = next_queue
			depth += 1
		}
		return reached
	end

	def dijkstra(start, setting = {})
		raise "invalid setting" if setting.class != Hash
		raise "invalid setting" if setting.keys - [:goal, :multi_source, :multi_sink, :max_dist] != []
		
		start = Set[start] if !setting[:multi_source]
		goal = setting[:goal]
		goal = Set[goal] if goal && !setting[:multi_sink]
		max_dist = setting[:max_dist]

		h = Heap.new
		reached = Set.new
		start.each{|v|
			h.add(v, 0)
			reached << v
		}

		dist = {}
		until h.empty?
			u, key = h.del_min
			return key if setting[:goal] && goal.include?(u)
			break if max_dist && key > max_dist
			dist[u] = key
			each_to_node_of(u){|v|
				d = dist[u] + edge_length(u, v)
				if reached.include?(v)
					h.update_key(v, d)
				else
					h.add(v, d)
					reached << v
				end
			}
		end
		goal ? nil : dist
	end

	def bidir_dijkstra(s, t)
		f_dist = {}
		b_dist = {}

		f_heap = Heap.new
		b_heap = Heap.new

		f_heap.add(s, 0)
		b_heap.add(t, 0)

		f_reached = Set[s]
		b_reached = Set[t]

		f_current_dist = b_current_dist = 0
		best = Float::INFINITY

		loop {
			if !f_heap.empty?
				u, key = f_heap.del_min
				f_dist[u] = key
				f_current_dist = key
				each_to_node_of(u){|v|
					d = f_dist[u] + edge_length(u, v)
					if b_dist[v]
						path_len = d + b_dist[v]
						best = path_len if path_len < best
					end
					if f_reached.include?(v)
						f_heap.update_key(v, d)
					else
						f_heap.add(v, d)
						f_reached << v
					end
				}
			end

			if !b_heap.empty?
				u, key = b_heap.del_min
				b_dist[u] = key
				b_current_dist = key
				each_from_node_of(u){|v|
					d = b_dist[u] + edge_length(v, u)
					if f_dist[v]
						path_len = d + f_dist[v]
						best = path_len if path_len < best
					end
					if b_reached.include?(v)
						b_heap.update_key(v, d)
					else
						b_heap.add(v, d)
						b_reached << v
					end
				}
			end

			return best if f_current_dist + b_current_dist >= best
			return nil if f_heap.empty? && b_heap.empty?
		}
	end
end

class Graph
	include Graphical
	require 'set'

	def add_node(label = nil)
		Node.new(label)
	end

	def del_node(u)
		u.neighbour.each_value{|e| del_edge(e)}
	end

	def add_edge(u, v, label = nil)
		e = Edge.new(u, v, label)
		u.neighbour[v] = e
		v.neighbour[u] = e
		e
	end
	
	def del_edge(e)
		u, v = e.from, e.to
		u.neighbour.delete(v)
		v.neighbour.delete(u)
		e
	end

	def contract(e)
		u, v = e.from, e.to
		new_node = add_node

		u.neighbour.each{|w, f1|
			f2 = v.neighbour[w]
			if f2
				f = yield(f1, f2)
				add_edge(new_node, w, f.length)
				del_edge(f2)
			else
				add_edge(new_node, w, f1.length)
			end
		}
		v.neighbour.each{|w, f|
			add_edge(new_node, w, f.length)
		}
		del_node(u)
		del_node(v)

		new_node
	end

	class Node
		def initialize(label)
			@label = label
			@neighbour = {}
		end

		def to_s
			@label
		end
		
		attr_accessor :neighbour
		attr_reader :label
	end

	class Edge
		def initialize(u, v, label = nil)
			@from = u	
			@to = v
			@label = label
		end
		attr_reader :from, :to, :label
	end


	def each_to_node_of(u)
		u.neighbour.each_key{|v|
			yield v
		}
	end
	alias :each_from_node_of :each_to_node_of

	def edge_length(u, v)
		u.neighbour[v] ? 1 : nil
	end

end

class WeightedGraph < Graph
	def add_edge(u, v, length, label = nil)
		e = Edge.new(u, v, length, label)
		u.neighbour[v] = e
		v.neighbour[u] = e
		e
	end

	class Edge
		def initialize(u, v, length, label = nil)
			@from = u	
			@to = v
			@length = length
			@label = label
		end
		attr_reader :from, :to, :length, :label
	end

	def edge_length(u, v)
		u.neighbour[v].length
	end

	def prim(start)
		h = Heap.new
		reached = Set.new
		h.add(start, 0)
		reached << start
		scanned = Set[start]
		used_edge = {}
		tree_len = 0

		until h.empty?
			u, key = h.del_min
			if e = used_edge[u]
				tree_len += e.length
			end
			scanned << u

			each_to_node_of(u){|v|
				next if scanned.include?(v)
				d = edge_length(u, v)
				if reached.include?(v)
					h.update_key(v, d)
					if d < used_edge[v].length
						used_edge[v] = u.neighbour[v]
					end
				else
					h.add(v, d)
					used_edge[v] = u.neighbour[v]
					reached << v
				end
			}
		end
		tree_len
	end
end

loop {
	g = WeightedGraph.new
	n = gets.to_i
	break if n == 0
	nodes = []
	edges = []
	dist = {}
	(1..n).each{
		a = gets.split.map &:to_f
		x1, y1, z1, r1 = a
		u = g.add_node(a)
		nodes.each{|v|
			x2, y2, z2, r2 = v.label
			d = Math.sqrt((x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2) - r1 - r2
			if d <= 0
				edges << g.add_edge(u, v, d) if d <= 0
			else
				dist[Set[u, v]] = d
			end
		}
		nodes << u
	}

	components = []

	until nodes.empty?
		components << g.reachable_from(nodes.pop)
		nodes -= components[-1].to_a
	end

	nodes = (1..components.size).map{ g.add_node }

	(0..nodes.size-2).each{|i|
		(i+1..nodes.size-1).each{|j|
			c1 = components[i]
			c2 = components[j]
			min = Float::INFINITY
			c1.each{|u|
				c2.each{|v|
					d = dist[Set[u, v]] 
					min = d if d < min
				}
			}
			g.add_edge(nodes[i], nodes[j], min)
		}
	}

	printf("%.3f\n", g.prim(nodes[0]))
}