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 LuaJIT (2.0.4) (judge verdict: Accepted)

local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local xs, ys, xt, yt = io.read("*n", "*n", "*n", "*n")
local n = io.read("*n")
local x, y, r = {}, {}, {}
x[1], y[1], r[1] = xs, ys, 0
for i = 1, n do
  x[i + 1], y[i + 1], r[i + 1] = io.read("*n", "*n", "*n")
end
x[n + 2], y[n + 2], r[n + 2] = xt, yt, 0
local n2 = n + 2
local len = {}
len[1] = 0
for i = 2, n2 do
  len[i] = 10000000000
end
local costs = {}
for i = 1, n2 * n2 do
  costs[i] = 10000000000
end

local function getcost(idx1, idx2)
  return mma(0, msq((x[idx1] - x[idx2]) * (x[idx1] - x[idx2]) + (y[idx1] - y[idx2]) * (y[idx1] - y[idx2])) - r[idx1] - r[idx2])
end

for i = 1, n + 1 do
  for j = i + 1, n + 2 do
    local c = getcost(i, j)
    costs[(i - 1) * n2 + j] = c
    costs[(j - 1) * n2 + i] = c
  end
end

local taskstate = {}
for i = 1, n2 do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = n2

local function addtask(idx)
  taskstate[idx] = true
  tasknum = tasknum + 1
  local taskidx = tasknum % tasklim
  if taskidx == 0 then taskidx = tasklim end
  tasks[taskidx] = idx
end
addtask(1)

while done < tasknum do
  done = done + 1
  local taskidx = done % tasklim
  if taskidx == 0 then taskidx = tasklim end
  local src = tasks[taskidx]
  taskstate[src] = false
  local at = {}
  for dst = 1, n2 do
    local c = costs[(src - 1) * n2 + dst]
    if len[src] + c < len[dst] then
      len[dst] = len[src] + c
      if dst ~= n2 then
        if not taskstate[dst] then
          table.insert(at, dst)
        end
      end
    end
  end
  table.sort(at, function(x, y) return len[x] < len[y] end)
  for i = 1, #at do addtask(at[i]) end
end
print(string.format("%.9f", len[n2]))