Find the minimum time for a pedestrian to travel across a grid of intersections where crossing depends on periodically timed traffic lights.
Medium7Shortest pathGraphSimulationNo attempts yetTime limit5sMemory limit512 MBThe city in this problem is a grid made of N roads running east to west and M roads running north to south. There is an intersection wherever an east-west road meets a north-south road, and every intersection has pedestrian lights. A pedestrian can cross a road only in the direction that has a green light.
Intersections are numbered from row 0 to row N−1 going from north to south, and from column 0 to column M−1 going from west to east.
The pedestrian wants to get from the northeast corner of the southwest block to the southwest corner of the northeast block. She starts at the southwest corner of intersection (N−1,0) and finishes at the northeast corner of intersection (0,M−1).
Two moves are available.
The pedestrian moves only along the edges of the blocks. She cannot go straight from one corner of a block to the opposite corner. She can wait at a corner as long as she likes.

Traffic lights repeat the following cycle. At intersection i the north-south lights stay green for Si minutes while the east-west lights stay red. Then the north-south lights turn red, the east-west lights turn green, and they stay that way for Wi minutes. Then the same cycle starts again. The pedestrian starts moving at t=0 minutes, and the lights at intersection i start a cycle by turning green in the north-south direction at t=Ti minutes. The same cycle repeats before t=Ti as well.
For example, suppose one intersection has S0=3, W0=2, T0=0. The north-south direction turns green at 0 minutes and stays green for 3 minutes, so during that time the pedestrian can cross in the north-south direction and not in the east-west direction. Then the lights switch, and for the next 2 minutes she can cross in the east-west direction only. Five minutes after it started, the cycle starts again. This is exactly the same as S0=3, W0=2, T0=10.
The first line contains the number of test cases, C. Then C test cases follow in the format below.
The first line of a test case contains N and M, the number of east-west roads (rows) and the number of north-south roads (columns). Then N lines follow. The ith of those lines describes the intersections in row i counted from the north, where row 0 is the northmost, and contains 3M integers separated by spaces in this order:
Si,0 Wi,0 Ti,0 Si,1 Wi,1 Ti,1 … Si,M−1 Wi,M−1 Ti,M−1
Si,j, Wi,j and Ti,j refer to the intersection in row i from the north and column j from the west.
Limits
For each test case, output one line containing Case #x: t, where x is the test case number and t is the minimum number of minutes the pedestrian needs to get from her starting corner to her destination.
In the first test case of the first example, the lights are the ones described in the statement. The pedestrian crosses to the north (1 minute), waits 2 minutes and then crosses to the east (1 minute), for a total of 4 minutes.
The second test case is shown in the diagram below. The pedestrian crosses to the east (1 minute), waits 2 minutes and crosses to the north (1 minute). Then she walks east one block (2 minutes) and crosses to the east (1 minute), for a total of 7 minutes.
