Problem:
Score : 600 points

Problem Statement

On the xy-plane, Snuke is going to travel from the point (x_s, y_s) to the point (x_t, y_t).
He can move in arbitrary directions with speed 1.
Here, we will consider him as a point without size.

There are N circular barriers deployed on the plane.
The center and the radius of the i-th barrier are (x_i, y_i) and r_i, respectively.
The barriers may overlap or contain each other.

A point on the plane is exposed to cosmic rays if the point is not within any of the barriers.

Snuke wants to avoid exposure to cosmic rays as much as possible during the travel.
Find the minimum possible duration of time he is exposed to cosmic rays during the travel.

Constraints

All input values are integers.

-10^9 ≤ x_s, y_s, x_t, y_t ≤ 10^9

(x_s, y_s) ≠ (x_t, y_t)

1≤N≤1,000

-10^9 ≤ x_i, y_i ≤ 10^9

1 ≤ r_i ≤ 10^9

Input

The input is given from Standard Input in the following format:

x_s y_s x_t y_t
N
x_1 y_1 r_1
x_2 y_2 r_2
:
x_N y_N r_N

Output

Print the minimum possible duration of time Snuke is exposed to cosmic rays during the travel.
The output is considered correct if the absolute or relative error is at most 10^{-9}.

Sample Input 1

-2 -2 2 2
1
0 0 1

Sample Output 1

3.6568542495

An optimal route is as follows:

Sample Input 2

-2 0 2 0
2
-1 0 2
1 0 2

Sample Output 2

0.0000000000

An optimal route is as follows:

Sample Input 3

4 -2 -2 4
3
0 0 2
4 0 1
0 4 1

Sample Output 3

4.0000000000

An optimal route is as follows:

Solution in Julia (0.5.0) (judge verdict: Wrong Answer)

parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function hpush(h::Array{Tuple{Int,Float64},1},x::Tuple{Int,Float64})
	push!(h,x)
	if length(h)>1
		l = length(h)
		k = l>>1
		while x[2]<h[k][2]&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k>>1
			l>>1
		end
	end
end

function hpop(h::Array{Tuple{Int,Float64},1})
	x = h[1]
	z = pop!(h)
	if !isempty(h)
		h[1] = z
		k = 1
		l = length(h)
		while k <= l
			if 2*k>l
				break
			elseif 2*k+1>l
				if h[2*k][2]<=z[2]
					break
				else
					tmp = h[2*k]
					h[2*k] = z
					h[k] = tmp
					k = 2*k
				end
			else
				if z[2]<=h[2*k][2]&&z[2]<=h[2*k+1][2]
					break
				else
					t = h[2*k][2]<=h[2*k+1][2]?2*k:2*k+1
					tmp = h[t]
					h[t] = z
					h[k] = tmp
					k = t
				end
			end
		end
	end
	x
end

dist(xa::Int,ya::Int,xb::Int,yb::Int,ra::Int,rb::Int)=sqrt((xa-xb)^2+(ya-yb)^2)-ra-rb

function main()
	xs,ys,xt,yt = readline() |> split |> parseMap
	n = readline() |> parseInt
	a = Tuple{Int,Int,Int}[(0,0,0) for i in 1:n]
	for i in 1:n
		x,y,r = readline() |> split |> parseMap
		a[i] = (x,y,r)
	end
	e = [Tuple{Int,Float64}[] for i in 1:n+2]
	for i in 1:n
		for j in i+1:n
			v = dist(a[i][1],a[i][2],a[j][1],a[j][2],a[i][3],a[j][3])
			push!(e[i], (j,max(0,v)))
			push!(e[j], (i,max(0,v)))
		end
		v = dist(a[i][1],a[i][2],xs,ys,a[i][3],0)
		push!(e[i],(n+1,max(0,v)))
		push!(e[n+1],(i,max(0,v)))
		v = dist(a[i][1],a[i][2],xt,yt,a[i][3],0)
		push!(e[i],(n+2,max(0,v)))
		push!(e[n+2],(i,max(0,v)))
	end
	v = dist(xs,ys,xt,yt,0,0)
	push!(e[n+1],(n+2,max(0,v)))
	push!(e[n+2],(n+1,max(0,v)))

	hs = Tuple{Int,Float64}[(n+1,0.0)]
	hg = Tuple{Int,Float64}[(n+2,0.0)]
	ds = -ones(Float64,n+2)
	dg = -ones(Float64,n+2)
	vst = zeros(Int,n+2)
	ds[n+1] = 0.0
	dg[n+2] = 0.0
	meet = 0
	while !isempty(hs)||!isempty(hg)
		v,w = hpop(hs)
		ds[v] = ds[v]<0.0?w:min(ds[v],w)
		vst[v] += 1
		if vst[v] > 1
			meet = v
			break
		end
		for i in 1:length(e[v])
			if ds[e[v][i][1]]<0.0 || ds[e[v][i][1]]>w+e[v][i][2]
				ds[e[v][i][1]] = w+e[v][i][2]
				hpush(hs,(e[v][i][1],w+e[v][i][2]))
			end
		end
		v,w = hpop(hg)
		dg[v] = dg[v]<0.0?w:min(dg[v],w)
		vst[v] += 1
		if vst[v] > 1
			meet = v
			break
		end
		for i in 1:length(e[v])
			if dg[e[v][i][1]]<0.0 || dg[e[v][i][1]]>w+e[v][i][2]
				dg[e[v][i][1]] = w+e[v][i][2]
				hpush(hg,(e[v][i][1],w+e[v][i][2]))
			end
		end
	end
	if ds[n+2]<0.0&&dg[n+1]<0.0
		println(ds[meet]+dg[meet])
	elseif ds[n+2]>=0.0
		if meet>0
			println(min(ds[n+2],ds[meet]+dg[meet]))
		else
			println(ds[n+2])
		end
	else
		if meet>0
			println(min(dg[n+1],ds[meet]+dg[meet]))
		else
			println(dg[n+1])
		end
	end
end

main()