Environment-Friendly Travel
Taking holidays unfortunately has a carbon footprint. The CO2-cost of a kilometre travelled depends on the mode of transportation. For instance, train travel is less costly than bus travel, which is itself less costly than car travel.
Your objective is to plan a holiday trip from your house, given by 2D coordinates (x[s]
, y[s]
), to your travel destination, given by (x[d]
, y[d]
). Being aware of the environmental impact of travel, you want to minimize the CO2-cost of your holiday, but you still want to keep the total number of kilometres travelled within a given budget B.
To aid you in planning, you have access to a map of n stations, possibly linked by t different modes of transportation (airplane, train, etc.) numbered 1, ..., t. Each mode has a CO2-cost per distance unit c[1]
, ..., c[t]
. You can travel by car from your home to the destination, from your home to any station, and from any station to the destination point, at a cost c[0]
per distance unit. c[0]
is always greater than any of c[1]
, ..., c[t]
.
Each of the n stations has coordinates (x[i]
, y[i]
) for i = 0, ..., n - 1. Each station may be connected to some other stations via one or several of the t modes. Each connection works both ways, so only one direction has to be listed. There can be multiple modes of transportation available between two stations. You can only travel between two stations via their connections using the available transportation modes (car travel is not allowed between stations).
The distance between two points a and b is the 2D distance between (x[a]
, y[a]
) and (x[b]
, y[b]
), rounded to the nearest integer above:
and the CO2-cost of travel between a and b, using transport mode i is:
cost(a, b, i) = c[i]
* dist(a, b)
Given two source–destination coordinates, a budget B expressed in distance units, a list of transportation modes and their respective CO2-costs, and the station network, your task is to compute the minimal CO2-cost possible while traveling at most B kilometres.
Input
Consists of the following lines:
Line 1 contains two space-separated integers
x[s]
andy[s]
, the coordinates of your house.Line 2 contains two space-separated integers
x[d]
andy[d]
, the coordinates of your destination.Line 3 contains an integer B (0 ≤ B ≤ 100), the maximal distance (in kilometres) that you accept to travel.
Line 4 contains an integer
c[0]
, the CO2-cost per distance unit of traveling by car.Line 5 contains an integer t (1 ≤ t ≤ 100), the number of modes of transportation (beside cars).
Line i + 5, for 1 ≤ i ≤ t, contains the integer
c[i]
, the CO2-cost per distance unit of transportation mode i (1 ≤c[1]
, ...,c[t]
<c[0]
≤ 100).Line t + 6 contains the integer n (1 ≤ n ≤ 1000), the number of stations.
Line i + t + 7 describes station i (0 ≤ i < n) and contains 3 + 2 *
l[i]
space-separated integers. The first three integers arex[i]
,y[i]
andl[i]
. The pair (x[i]
,y[i]
) represents the coordinates of station i, whilel[i]
is the number of links between station i and other stations. Thel[i]
remaining pairs of integers (j,m[j]
) each describe a link to station j (0 ≤ j < n) using transportation modem[j]
(1 ≤m[j]
≤ t). All links are bidirectional.
All inputs are integers. All coordinates are in [0, 100] * [0, 100]. For each station, there are at most 100 links to other stations.
Output
Print one integer representing the minimal feasible CO2-cost or -1 if no feasible cost is found within the kilometre budget.
Examples
Note
The results corresponds to the CO2-cost of the following route:
from home at (1, 1) to station 0 at (2, 3) by car for a cost of 100 * sqrt(
1^2
+2^2
) = 300,to station 2 at (9, 3) via transportation mode 2 for a cost of 50 * sqrt(
7^2
+0^2
) = 350,to the destination at (10, 2) by car for a cost of 100 * sqrt(
1^2
+1^2
) = 200 for a total of 850.
This route is valid because the total distance travelled is 3 + 7 + 2 = 12, within the allowed budget of 12.