Crossing the Road (Small)
Time limit5sMemory limit512 MB
On a tiny grid whose intersections have periodic pedestrian lights, find the minimum time to walk from the southwest corner of the grid to the northeast corner.
- Level
Medium5 of 10
- Topics
- Shortest path, Graph, Simulation
- Solved
- No attempts yet
Problem
Where roads intersect there are traffic lights that tell pedestrians when they may cross. A clever pedestrian plans her route through the city around the moments those lights turn green.
The city in this problem is a grid of roads running east to west and roads running north to south, so it has intersections. The pedestrian wants to get from the northeast corner of the southwest block to the southwest corner of the northeast block. In other words, she starts at the southwest corner of the southwesternmost intersection and finishes at the northeast corner of the northeasternmost intersection. Find the smallest number of minutes she needs to get from the start corner to the goal corner.
Crossing one road takes 1 minute, and the light for that direction must be green for the entire crossing. Every intersection has four corners, and one crossing moves the pedestrian to an adjacent corner of the same intersection. Walking along one edge of a block, between two neighboring intersections, takes 2 minutes and needs no light. The pedestrian moves only along the edges of a block; she cannot cut diagonally from one corner of a block to the opposite corner.

Traffic lights follow this pattern. At intersection the north-south lights stay green for 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 minutes. Then the same cycle starts again. The pedestrian starts moving at minutes, and the cycle at intersection starts by turning green in the north-south direction at minutes. The same cycle also repeats before .
For example, suppose intersection 0 has , , . The north-south direction turns green at minute 0 and stays green for 3 minutes, and during those 3 minutes the pedestrian can cross in the north-south direction only. 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 configuration is exactly the same as , , .
Input
The first line contains the number of test cases, . Then test cases follow, each in this format.
The first line of a test case contains and , the number of east-west roads and the number of north-south roads. Then lines follow. The th of those lines describes the intersections on the th row from the north, where the northmost row is row 0. Each of those lines contains integers separated by spaces, in this order.
S[i][0] W[i][0] T[i][0] S[i][1] W[i][1] T[i][1] ... S[i][M-1] W[i][M-1] T[i][M-1]
, and belong to the intersection in the th row from the north and the th column from the west.
Limits
- , , , , and are all non-negative integers.
Output
For each test case, output one line containing Case #x: t, where is the number of the test case and is the minimum number of minutes the pedestrian needs to get from the start corner to the goal corner.
Note
The first case of the first example uses the light setting described above. The pedestrian crosses to the north (1 minute), waits 2 minutes, then crosses to the east (1 minute), for a total of 4 minutes.
The second case is drawn 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 again (1 minute), for a total of 7 minutes.
