#P11952. Shortest Path on a Grid with Temporary Edge Removal
Shortest Path on a Grid with Temporary Edge Removal
Shortest Path on a Grid with Temporary Edge Removal
Given an grid graph, each point has two possible outgoing directed edges:\
- If , there is an edge from to with weight .\
- If , there is an edge from to with weight .\
The task is to compute the shortest path from to . In addition, there are queries. In each query, an edge is temporarily removed from the grid. Specifically, if the removed edge is horizontal (denoted by 'R'), then the edge from to is removed; if it is vertical (denoted by 'D'), then the edge from to is removed. After computing the shortest path (or reporting (-1) if no path exists), the removed edge is restored for the next query.
Your task is to output the shortest path cost for every query under these conditions.
inputFormat
The input is given as follows:\n\n- The first line contains an integer , the size of the grid.\n- The next lines each contain integers. The -th integer in the -th line is , the weight of the edge from to .\n- The following lines each contain integers. The -th integer in the -th line is , the weight of the edge from to .\n- The next line contains an integer , the number of queries.\n- Each of the next lines contains a character and two integers: the character is either 'R' or 'D', indicating whether a rightward (horizontal) or downward (vertical) edge is to be temporarily removed, and the two integers and denote the starting coordinates of the edge to be removed.
outputFormat
For each query, output the shortest path cost from to after the specified edge is removed. If there is no valid path, output (-1).
sample
3
1 2
3 4
5 6
7 8 9
10 11 12
2
R 1 1
D 2 2
20
20
</p>