#P9550. 「PHOI-1」晚宴筵

「PHOI-1」晚宴筵

Background

After traveling a long distance in City Z, Xiao X is going to attend someone else's banquet.

Problem Description

City Z is an n×nn \times n matrix. Xiao X plans to reach the banquet using only a teleporter and a time-space traveler. If Xiao X is currently at position (p,q)(p,q) satisfying l1xpr1xl1_x \le p \le r1_x and l2yqr2yl2_y \le q \le r2_y (0l1xr1x<x,0l2yr2y<y)(0 \le l1_x \le r1_x < x,0 \le l2_y \le r2_y < y), then Xiao X can reach position (x,y)(x,y).

However, because the teleport technology is not very mature and due to the influence of the time-space traveler, each teleport costs wp+wq+wx+wypqxyw_p+w_q+w_x+w_y-p-q-x-y seconds (because of the nature of the time-space traveler, the time may be negative). If an index is not a positive integer, the teleporter will be damaged, so Xiao X can only reach positions whose indices are all positive integers.

Now Xiao X is at (1,1)(1,1). He wants to attend the banquet, but he does not know where it is. So he wants you to compute the minimum time needed to reach every position (x,y)(x,y) (1x,yn)(1 \le x,y \le n). If a position cannot be reached, output inf.

Input Format

Line 11 contains an integer nn, the size of the matrix.

Line 22 contains nn non-negative integers: l11,l12l1nl1_1,l1_2 \ldots l1_n.

Line 33 contains nn non-negative integers: r11,r12r1nr1_1,r1_2 \ldots r1_n.

Line 44 contains nn non-negative integers: l21,l22l2nl2_1,l2_2 \ldots l2_n.

Line 55 contains nn non-negative integers: r21,r22r2nr2_1,r2_2 \ldots r2_n.

Line 66 contains nn non-negative integers: w1,w2wnw_1,w_2 \ldots w_n.

The testdata guarantees that l1ir1il1_i \le r1_i and l2ir2il2_i \le r2_i.

Output Format

Output nn lines, each containing nn integers. The integer in row ii and column jj denotes the shortest path length from (1,1)(1,1) to (i,j)(i,j).

3
0 1 1
0 1 2
0 0 2
0 1 2
2 0 4
0 inf inf
inf -2 inf
inf 1 -4
10
0 1 1 2 2 2 3 3 3 3
0 1 2 2 3 3 3 4 4 4
0 1 2 2 3 3 3 3 4 4
0 1 2 3 4 4 5 5 5 5
8 4 2 1 2 4 8 4 2 1
0 inf inf inf inf inf inf inf inf inf
inf 18 inf inf inf inf inf inf inf inf
inf 15 20 18 inf inf inf inf inf inf
inf inf 18 16 inf inf inf inf inf inf
inf inf 12 10 8 9 12 7 4 2
inf inf 13 11 9 10 13 8 5 3
inf inf 16 14 12 13 16 11 8 6
inf inf 11 7 3 4 7 2 -1 -3
inf inf 8 4 0 1 4 -1 -4 -6
inf inf 6 2 -2 -1 2 -3 -6 -8

Hint

This problem uses bundled testcases.

Subtask nn l1i,l2il1_i,l2_i wiw_i Score
00 1n701 \le n \le 70 No special constraints iwi105(1in)i \le w_i \le 10^5(1 \le i \le n) 1515
11 No special constraints 2525
22 No special constraints l1i=l2i=r1i=r2i=1(2in)l1_i=l2_i=r1_i=r2_i=1(2 \le i \le n) 55
33 No special constraints 5555

For 100%100\% of the testdata, it is guaranteed that 1n1031 \le n \le 10^3, 0l1ir1in0 \le l1_i \le r1_i \le n, 0l2ir2in0 \le l2_i \le r2_i \le n, 0wi1050 \le w_i \le 10^5, and l1ir1i<il1_i \le r1_i < i, l2ir2i<il2_i \le r2_i < i.

Sample Explanation #1:

  • From (1,1)(1,1) to (1,1)(1,1) obviously takes 00 seconds.

  • From (1,1)(1,1) you can directly reach (2,2)(2,2), costing w1+w1+w2+w21122=2w_1 + w_1 + w_2 + w_2 - 1 - 1 - 2 - 2 = -2 seconds.

  • From (1,1)(1,1) you can also directly reach (3,2)(3,2), costing w1+w1+w3+w21132=1w_1 + w_1 + w_3 + w_2 - 1 - 1 - 3 - 2 = 1 seconds.

  • To reach (3,3)(3,3) from (1,1)(1,1), you need to go from (1,1)(1,1) to (2,2)(2,2) first, then from (2,2)(2,2) to (3,3)(3,3). The cost is $w_1 + w_1 + w_2 + w_2 - 1 - 1 - 2 - 2 + w_2 + w_2 + w_3 + w_3 - 2 - 2 - 3 - 3 = -4$ seconds.

  • By manual calculation, you can find that from (1,1)(1,1) you cannot reach any other position.

Translated by ChatGPT 5