This page is still under construction.

Parts of this page are still being built. What you see may change.

Journey through the kingdom

Time limit3sMemory limit256 MB

Summary
Find the cheapest carriage-hopping cost between consecutive target cells on a grid where each cell rents a ride to any cell in its rectangle range.
Level

Hard8 of 10

Topics
Shortest path, Segment tree
Solved
No attempts yet

Problem

The kingdom of Quadradonia is divided into provinces arranged in a grid of RR rows and CC columns. The roads between them are dangerous, so nobody travels alone. Every trip goes by escorted carriage, and the carriages belong to the Interprovincial Communication and Peregrination Company (ICPC).

The company charges like this. In the province in row ii and column jj you rent a carriage for a cost of VijV_{ij}. That carriage takes you to any province at most RijR_{ij} rows away from row ii and at most CijC_{ij} columns away from column jj, that is, to the province in row i′i' and column j′j' whenever ∣i−i′∣≤Rij|i - i'| \le R_{ij} and ∣j−j′∣≤Cij|j - j'| \le C_{ij}. The price is flat: it depends on the province where you rent, not on where you get off.

You want to visit NN provinces p1,p2,…,pNp_1, p_2, \dots, p_N in that order. Your budget is small, so for every leg of the trip you want the cheapest way to make it. A leg may pass through any number of intermediate provinces, and you pay the rental cost of every province where you board a carriage.

Input

The first line contains three integers RR, CC and NN (1≤R,C≤5001 \le R, C \le 500, 2≤N≤52 \le N \le 5), the number of rows, the number of columns and the number of provinces you want to visit. Rows are numbered from 1 to RR and columns from 1 to CC.

The next 3×R3 \times R lines form three groups of RR lines, each line holding CC integers. The jj-th number on the ii-th line of the first group is VijV_{ij} (1≤Vij≤10001 \le V_{ij} \le 1000). The second group gives RijR_{ij} (0≤Rij≤R0 \le R_{ij} \le R) in the same layout, and the third group gives CijC_{ij} (0≤Cij≤C0 \le C_{ij} \le C).

The last NN lines describe p1,p2,…,pNp_1, p_2, \dots, p_N in visiting order. The kk-th of them contains two integers IkI_k and JkJ_k (1≤Ik≤R1 \le I_k \le R, 1≤Jk≤C1 \le J_k \le C), meaning that pkp_k is the province in row IkI_k and column JkJ_k.

Output

Print one line with N−1N - 1 integers separated by single spaces. For k=1,2,…,N−1k = 1, 2, \dots, N - 1, the kk-th number is the minimum total rental cost of going from pkp_k to pk+1p_{k+1} with the escorted carriage system, or −1-1 if that leg is impossible. If pkp_k and pk+1p_{k+1} are the same province the cost is 0.

Examples2

  1. Example 1

    Input
    3 4 5
    1 2 1 1
    1 5 3 4
    1 1 6 3
    1 2 3 3
    3 3 1 2
    0 0 0 1
    1 4 0 1
    2 3 0 1
    4 1 3 1
    1 1
    3 4
    1 1
    2 2
    2 2
    
    Expected output
    3 -1 1 0
  2. Example 2

    Input
    1 5 3
    9 1 1 1 1
    0 0 0 0 0
    4 1 1 1 1
    1 1
    1 5
    1 1
    
    Expected output
    9 4