Safest Taxi

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

Consider a town whose road network forms an N\*MN \* M grid, where adjacent intersections are connected by roads. All roads are bi-directional. Each direction has an associated number - the time needed to travel from one end-point to another.

Each direction of each road consists of one or more lanes. A lane can serve one of the following functions: left-turn, straight, right-turn, or any combination of them. However, a left-turn lane cannot be placed to the right of a straight or right-turn lane, and a straight lane cannot be placed to the right of a right-turn lane. There are no U-turn lanes.

The rules for crossing intersections are illustrated in the above figure (suppose a car enters the intersection from the south). To make a left turn, it must be in one of the LL left-turn lanes; let's number them 11 through LL from left to right. The traffic rule says Lane ii must turn into the ii-th lane (counting from the left) of the target road, except that Lane LL may turn into the LL-th lane or any other lanes to its right. 

Similarly, to go straight through an intersection, the car must be in one of the SS straight lanes; let's number them 11 through SS from left to right. Lane ii must go into the ii-th lane (counting from the left) of the target road, except that Lane SS may go into the SS-th lane or any other lanes to its right.

To make a right turn, the car must be in one of the RR right-turn lanes. For the convenience of discussion, we consider these lanes and those of the target road from right to left. Let's number the right-turn lanes 11 through RR from right to left. Lane ii must turn into the ii-th lane (counting from the right) of the target road, except that Lane RR may turn into the RR-th lane or any other lanes to its left.

It is guaranteed that if at least one left-turn / straight / right-turn lane is present, the target road must exist and have enough lanes to accommodate the left turn / straight / right turn, respectively. The time spent on crossing intersections is negligible.

In addition, a driver may change lanes in the middle of a road. Note that in the above rules for intersections, it doesn't count as a lane change to drive into any of the legal lanes of the target road. The time spent on lane changes is negligible.

A trip starts and ends at the rightmost lane of the midpoint of roads. The time needed to travel midpoint-to-endpoint is half of endpoint-to-endpoint.

You are running a taxi company called "Safest Taxi" in this town, with the slogan "your safety is in your hands". You let your customers choose the numbers XX and YY for their trip, and the driver will make at most XX left turns and YY lane changes to accomplish the trip.

What is the shortest time to fulfill each trip given the rules?

입력

The first line consists of three integers NN (2N152 \leq N \leq 15), MM (2M152 \leq M \leq 15) and KK (1K31 \leq K \leq 3), separated by a single space. The town's road network has NN intersections north-south and MM intersections west-east. Each road has KK lanes.

The second line consists of a single integer DD. The town's road network has DD road segments. Every adjacent pair of intersections must appear in the list exactly once.

Each of the next DD lines describes a road segment with the following format:

R_0;C_0;R_1;C_1;T;L_0;L_1...L_K1R\_0\\;C\_0\\;R\_1\\;C\_1\\;T\\;L\_0\\;L\_1 ... L\_{K-1}

This describes a road segment going from the intersection at row R_0R\_0 column C_0C\_0 to the intersection at row R_1R\_1 column C_1C\_1 (0R_0,R_1\<N0 \leq R\_0,R\_1\<N, 0C_0,C_1\<M0 \leq C\_0,C\_1\<M). Rows are numbered 00 through N1N-1 from north to south, and columns are numbered 00 through M1M-1 from west to east. The segment must connect two adjacent intersections, i.e., R_0R_1+C_0C_1 =1\mid R\_0 - R\_1 \mid + \mid C\_0 - C\_1\mid  = 1. The time to travel through the entire segment is TT (2T1002 \leq T \leq 100, TT must be an even number). The next KK strings describe the function of each of the KK lanes, from left to right, with the following semantics:

  • L \mid Left-turn only
  • S \mid Straight only
  • R \mid Right-turn only
  • LR \mid Left-turn or right-turn
  • LS \mid Left-turn or straight
  • SR \mid Straight or right-turn
  • LSR \mid Left-turn, straight or right-turn

The next line consists of a single integer PP (1P501 \leq P \leq 50), the number of trips to fulfill.

Each of the next PP lines describes a trip with the following format:

R_S0;C_S0;R_S1;C_S1;R_D0;C_D0;R_D1;C_D1;X;YR\_{S0}\\;C\_{S0}\\;R\_{S1}\\;C\_{S1}\\;R\_{D0}\\;C\_{D0}\\;R\_{D1}\\;C\_{D1}\\;X\\;Y

This indicates that the starting point is the midpoint of segment (R_S0,C_S0R\_{S0}, C\_{S0}) \to (R_S1,C_S1R\_{S1}, C\_{S1}), and the destination is the midpoint of segment (R_D0,C_D0R\_{D0}, C\_{D0}) \to (R_D1,C_D1R\_{D1}, C\_{D1}). Both segments must appear in the above list. Both the starting point and the destination are on the rightmost lane. The customer requests that at most XX (0X40 \leq X \leq 4) left turns and YY (0Y40 \leq Y \leq 4) lane changes are allowed for the trip.

출력

Output PP lines. The ii-th line contains a single integer which is the shortest time to fulfill each trip given the rules, or 1-1 if no feasible route exists.

힌트

The first three lines of the sample output are illustrated in the figure below.

  • If X=1X = 1 and Y=1Y = 1, the shortest path is shown in red: make a lane change before reaching E and make a left turn. The total time is 8/2+8/2=88/2+8/2=8;
  • If X=1X = 1 and Y=0Y = 0, the shortest path is shown in green: go through E-F-I-H-E and make a left turn. The total time is 8/2+16+8+8+8+8/2=488/2+16+8+8+8+8/2=48;
  • If X=0X = 0 and Y=0Y = 0, the shortest path is shown in blue: go through E-B-C-F-E. The total time is 8/2+16+16+8+18+8/2=668/2+16+16+8+18+8/2=66.