#P4150. [WC2009] 最短路问题

    ID: 3088 远端评测题 6000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2009线段树WC/CTSC/集训队O2优化

[WC2009] 最短路问题

Description

A 6×n6 \times n grid is given, where each cell initially has a non-negative weight. There are two types of operations:

  • Change the weight of a cell (the new weight is still non-negative).
  • Query the weight of the shortest path between two cells.

Notes and task:

For any cell PP, its coordinates (xP,yP)(x_P, y_P) satisfy 1xP61 \leq x_P \leq 6, 1yPn1 \leq y_P \leq n. The Manhattan distance between cells PP and QQ is defined as xPxQ+yPyQ|x_P - x_Q| + |y_P - y_Q|. An ordered sequence of cells (p1,p2,,pk)(p_1, p_2, \ldots, p_k) is called a path from p1p_1 to pkp_k if the Manhattan distance between any pip_i and pi+1p_{i+1} is 11. Its weight is d(p1)+d(p2)++d(pk)d(p_1) + d(p_2) + \cdots + d(p_k), where d(P)d(P) is the weight of cell PP. The shortest path between two cells PP and QQ is a path from PP to QQ with the minimum total weight.

Input Format

The first line contains an integer nn. The next 66 lines each contain nn integers; on the (i+1)(i+1)-th line, the jj-th integer is the initial weight of cell (i,j)(i, j). Then an integer QQ follows, and the next QQ lines each describe an operation.

There are two types of operations:

Type 11: "1 x y c" (without double quotes). Set the weight of cell (x,y)(x, y) to cc (1x61 \leq x \leq 6, 1yn1 \leq y \leq n, 0c100000 \leq c \leq 10000).

Type 22: "2 x_1 y_1 x_2 y_2" (without double quotes). Query the weight of the shortest path between cell (x1,y1)(x_1, y_1) and cell (x2,y2)(x_2, y_2) (1x1,x261 \leq x_1, x_2 \leq 6, 1y1,y2n1 \leq y_1, y_2 \leq n).

Output Format

For each type 22 operation, in the order they appear, output one line with one integer: the weight of the shortest path.

5
0 0 1 0 0
0 1 0 1 0
0 2 0 1 0
0 1 1 1 0
0 0 0 0 0
1 1 1 1 1
5
2 1 2 1 4
1 1 1 10000
2 1 2 1 4
1 2 3 10000
2 1 2 3 3
0
1
2

Hint

Testdata ID nn QQ Testdata ID nn QQ
11 1010 2020 66 10410^4 3×1043\times 10^4
22 100100 200200 77 3.5×1043.5\times 10^4
33 10310^3 2×1032\times 10^3 88 5×1045\times 10^4
44 10410^4 99 10510^5 6×1046\times 10^4
55 1010 10510^5

2024/08/20: Added 3 sets of hack testdata.

Translated by ChatGPT 5