#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 matrix. Xiao X plans to reach the banquet using only a teleporter and a time-space traveler. If Xiao X is currently at position satisfying and , then Xiao X can reach position .
However, because the teleport technology is not very mature and due to the influence of the time-space traveler, each teleport costs 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 . 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 . If a position cannot be reached, output inf.
Input Format
Line contains an integer , the size of the matrix.
Line contains non-negative integers: .
Line contains non-negative integers: .
Line contains non-negative integers: .
Line contains non-negative integers: .
Line contains non-negative integers: .
The testdata guarantees that and .
Output Format
Output lines, each containing integers. The integer in row and column denotes the shortest path length from to .
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 | Score | |||
|---|---|---|---|---|
| No special constraints | ||||
| No special constraints | ||||
| No special constraints | ||||
| No special constraints |
For of the testdata, it is guaranteed that , , , , and , .
Sample Explanation #1:
-
From to obviously takes seconds.
-
From you can directly reach , costing seconds.
-
From you can also directly reach , costing seconds.
-
To reach from , you need to go from to first, then from to . 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 you cannot reach any other position.
Translated by ChatGPT 5