eolymp
bolt
Try our new interface for solving problems
Problems

Environment-Friendly Travel

Environment-Friendly Travel

Time limit 1 second
Memory limit 128 MiB

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:

prb9702.gif

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 data

Consists of the following lines:

  • Line 1 contains two space-separated integers x[s] and y[s], the coordinates of your house.

  • Line 2 contains two space-separated integers x[d] and y[d], the coordinates of your destination.

  • Line 3 contains an integer B (0B100), 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 (1t100), the number of modes of transportation (beside cars).

  • Line i + 5, for 1it, contains the integer c[i], the CO2-cost per distance unit of transportation mode i (1c[1], ..., c[t] < c[0]100).

  • Line t + 6 contains the integer n (1n1000), the number of stations.

  • Line i + t + 7 describes station i (0i < n) and contains 3 + 2 * l[i] space-separated integers. The first three integers are x[i], y[i] and l[i]. The pair (x[i], y[i]) represents the coordinates of station i, while l[i] is the number of links between station i and other stations. The l[i] remaining pairs of integers (j, m[j]) each describe a link to station j (0j < n) using transportation mode m[j] (1m[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 data

Print one integer representing the minimal feasible CO2-cost or -1 if no feasible cost is found within the kilometre budget.

Examples

Input example #1
1 1
10 2
12
100
2
10
50
3
2 3 2 1 1 2 2
5 5 1 2 1
9 3 0
Output example #1
850

Note

The results corresponds to the CO2-cost of the following route:

  1. from home at (1, 1) to station 0 at (2, 3) by car for a cost of 100 * sqrt(1^2 + 2^2) = 300,

  2. to station 2 at (9, 3) via transportation mode 2 for a cost of 50 * sqrt(7^2 + 0^2) = 350,

  3. 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.

Source 2019 ACM Southwestern Europe Regional Contest (SWERC), Paris, January 26 (2020), Problem A