Traffic Signals
Time limit8sMemory limit512 MB
On a grid of streets with individually timed traffic lights, find the driving time of the fastest route from home to a friend's house, waiting at red lights.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Simulation
- Solved
- No attempts yet
Problem
You live in the city of Kyoot. Every street there is laid out on a grid. The streets that run from north to south are called avenues, and the streets that run from east to west are called drives.
Avenues are numbered from the west, so the westernmost one is avenue 1 and the next one to the east is avenue 2. Drives are numbered from the south in the same way. The figure below shows the layout.

Every crossing point of an avenue and a drive has a traffic signal. A green light means you may go, and a red light means you must stop there. If your direction is red when you reach an intersection, you wait until it turns green. Reaching the intersection at the exact moment the light turns red counts as red, so you wait. Reaching it at the exact moment the light turns green counts as green, so you pass without waiting.
The two directions of one signal always show different colors. When the light for the north-south direction is green, the light for the east-west direction is red, and the other way around. The two directions are never green together, which would cause crashes, and never red together either, which would waste time. The moment one turns red, the other turns green. Each signal repeats its own fixed cycle forever.

The light that applies to you is the one for the direction you are driving as you reach the intersection. If you arrive along an avenue you obey the north-south light, and if you arrive along a drive you obey the east-west light. Once your light is green you cross the intersection without waiting, whether you go straight, turn left, or turn right.
Tomorrow you drive to a friend's house. You want to get there as early as possible, so you take the route that costs the least time. Given the road map and the setting of every signal, compute the shortest time from your house to your friend's house.
Your car covers one unit of distance in one unit of time. Turning, starting, and stopping take no time. Ignore other cars. You leave at time 0.
Input
The input holds several test cases. Each test case has this format:
w h
dA(1) dA(2) ... dA(w-1)
dD(1) dD(2) ... dD(h-1)
ns(1,1) ew(1,1) s(1,1)
...
ns(w,1) ew(w,1) s(w,1)
ns(1,2) ew(1,2) s(1,2)
...
ns(w,h) ew(w,h) s(w,h)
xs ys
xd yd
The first line holds two integers and , the number of avenues and the number of drives (). The next two lines hold and integers. is the distance between avenue and avenue , and is the distance between drive and drive ().
The next lines describe the signals. The three integers , and belong to the signal at the crossing of avenue and drive . is how long the north-south direction stays green and is how long the east-west direction stays green (). is the state at time 0: 0 means the north-south direction is green, 1 means the east-west direction is green. Every signal has just switched to state at time 0.
The last two lines hold your house at and your friend's house at . Coordinates are measured from the crossing of avenue 1 and drive 1, which sits at . The x-axis is parallel to the drives and the y-axis is parallel to the avenues. The x-coordinate grows toward the east and the y-coordinate grows toward the north. Both houses stand on a street between two neighboring intersections, and neither one coincides with an intersection.
All values are integers separated by blanks.
A line with two zeros marks the end of the input. That line is not a test case and is not processed. The input holds at most 30 test cases.
Output
For each test case, print the shortest time from your house to your friend's house on its own line.