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: Time Limit Exceeded)

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

function eval(a::Tuple{Int,Float64},b::Tuple{Int,Float64})
	if a[2]<b[2]
		1
	elseif a[2]==b[2]
		0
	else
		-1
	end
end
 
function hpush(h::Array{Tuple{Int,Float64},1},x::Tuple{Int,Float64},n::Int)
	h[n+1]=x
	if n+1 > 1
		l = n+1
		k = l>>1
		while x[2]<h[k][2]&&k>=1
			tmp = h[k]
			h[k] = x
			h[l] = tmp
			k = max(k>>1,1)
			l = l>>1
		end
	end
	h
end
 
function hpop(h::Array{Tuple{Int,Float64},1},n::Int)
	x = h[1]
	z = h[n]
	if n>1
		h[1] = z
		k = 1
		l = n-1
		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) for i in 1:(n+2)^2]
	ls = 1
	hg = Tuple{Int,Float64}[(n+2,0.0) for i in 1:(n+2)^2]
	lg = 1
	ds = -ones(Float64,n+2)
	dg = -ones(Float64,n+2)
	vss = zeros(Int,n+2)
	vsg = zeros(Int,n+2)
	ds[n+1] = 0.0
	dg[n+2] = 0.0
	while !isempty(hs)||!isempty(hg)
		v,w = hpop(hs,ls)
		ls-=1
		ds[v] = ds[v]==-1.0?w:min(ds[v],w)
		vss[v] = 1
		if vss[v]+vsg[v] > 1
			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]),ls)
				ls += 1
			end
		end
		vv,ww = hpop(hg,lg)
		lg-=1
		dg[vv] = dg[vv]==-1.0?ww:min(dg[vv],ww)
		vsg[vv] = 1
		if vss[vv]+vsg[vv] > 1
			break
		end
		for i in 1:length(e[vv])
			if dg[e[vv][i][1]]<0.0 || dg[e[vv][i][1]]>ww+e[vv][i][2]
				dg[e[vv][i][1]] = ww+e[vv][i][2]
				hpush(hg,(e[vv][i][1],ww+e[vv][i][2]),lg)
				lg+=1
			end
		end
	end
	minim = ds[1]+dg[1]
	for i in 2:n+2
		if ds[i]>-1.0 && dg[i]>-1.0
			minim = min(minim,ds[i]+dg[i])
		end
	end
	println(minim)
end

main()