#P11952. Shortest Path on a Grid with Temporary Edge Removal

    ID: 14061 Type: Default 1000ms 256MiB

Shortest Path on a Grid with Temporary Edge Removal

Shortest Path on a Grid with Temporary Edge Removal

Given an n×nn \times n grid graph, each point (x,y)(x,y) has two possible outgoing directed edges:\

  1. If y<ny<n, there is an edge from (x,y)(x, y) to (x,y+1)(x, y+1) with weight eax,yea_{x,y}.\
  2. If x<nx<n, there is an edge from (x,y)(x,y) to (x+1,y)(x+1, y) with weight ebx,yeb_{x,y}.\

The task is to compute the shortest path from (1,1)(1,1) to (n,n)(n,n). In addition, there are qq 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 (x,y)(x,y) to (x,y+1)(x, y+1) is removed; if it is vertical (denoted by 'D'), then the edge from (x,y)(x,y) to (x+1,y)(x+1,y) 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 nn, the size of the grid.\n- The next nn lines each contain n1n-1 integers. The jj-th integer in the ii-th line is eai,jea_{i,j}, the weight of the edge from (i,j)(i,j) to (i,j+1)(i,j+1).\n- The following n1n-1 lines each contain nn integers. The jj-th integer in the ii-th line is ebi,jeb_{i,j}, the weight of the edge from (i,j)(i,j) to (i+1,j)(i+1,j).\n- The next line contains an integer qq, the number of queries.\n- Each of the next qq 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 xx and yy denote the starting coordinates of the edge to be removed.

outputFormat

For each query, output the shortest path cost from (1,1)(1,1) to (n,n)(n,n) 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>