Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
Statistical Charts
Submit Problem
Online Status
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Joy of Mobile Routing
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 240Accepted: 70


A computer professor, who is fond of programming, has come to Tehran to pay a visit to our regional contest. After hearing about such an event, he has tried his best to be able to participate in the contest as a visitor. He would decide to hold such contests in his university, if he finds it fun enough.
As could be predicted, he has been lost in our large and crowded city, Tehran. He does not know where to go. Getting really tired, he is standing in a city intersection when he suddenly remembers the phone number of a friend in the organizing committee, and instantly calls him. The friend understands the situation and wants to route the professor through mobile phone calls. He plans to give the professor directions at each intersection; i.e. he tells the tired professor in which direction to move, and after reaching the next intersection, there is another phone call, to get the new direction, etc. This process continues, until the professor sees his friend at the destination intersection, waiting for him. Due to poor telecommunications installment, not all city intersections are accessible through mobile network.
The professor should get to the regional contest location as soon as possible. There are a number of antennas located in some of city intersections. A city intersection is accessible through the network if an antenna can see the point; i.e. there is an imaginary straight line connecting the intersection to any part of the antenna. The line can never cross a building, but touching a boundary does not block the mobile connection. The city is composed of R*C rectangular blocks. Each block is a building having 10 meters width and length.
You should write a program to find out the shortest possible mobile route; i.e. a route that lets the professor call his friend from each intersection, until reaching the destination. The program is given R, C and height of each building and each antenna, and the location of antennas.


The only number of the first line, T, (1 <= T <= 20) is the number of different test cases to be solved. T input blocks follow, which describe different independent test cases.
The first line of a block, contains two integers R, C (1 <= R,C <= 50), the number of rows and columns of the city map, respectively. Next R lines, each contains C non-negative integers, 0 <= Hij <= 1000, describing the building heights. The first entry is the leftmost building in the uppermost row. The coordinates of the starting and ending points are given next; the row coordinates come first in each line.
The lower and rightmost intersection is (R,C), while the opposite intersection is (0, 0).
Followed is A, (0 <= A <= 100), the number of mobile antennas. Next A lines describe the antennas by three numbers, r, c, (0 <= r <= R, 0 <= c <= C), and h, (0 <= h <= 1000). It means that there is an antenna with height h at the intersection (r, c).


For each block of the input, write a single integer, which is the least distance the professor is to travel before getting to the contest, in meters. If there is no mobile route, and the professor’s friend is to choose another way for the routing, output -1 instead.

Sample Input

3 2
0 10
20 15
5 4
3 0
1 2
0 0 6

Sample Output



[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator