luogu#P7916. [CSP-S 2021] 交通规划

    ID: 7270 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2021网络流O2优化最短路平面图CSP-S 提高级

[CSP-S 2021] 交通规划

Problem Description

Given nn horizontal lines and mm vertical lines on a plane, they intersect to form a grid with nn rows and mm columns. The intersection point between the rr-th horizontal line from top to bottom and the cc-th vertical line from left to right is called the grid point (r,c)(r, c). In the grid, the segment between any two horizontally or vertically adjacent grid points is called an edge, and each edge has a non-negative integer weight.

There are TT queries. Each query is as follows:

You are given kk (the value of kk may differ among the TT queries) additional points. Each additional point lies on a ray that starts from the border of the grid and goes outward. All rays starting from the border of the grid are numbered from 11 to 2n+2m2 n + 2 m in the order top-left \to top-right \to bottom-right \to bottom-left \to top-left, as shown in the figure:

For each query, the rays on which different additional points lie are all distinct. The segment between each additional point and its nearest grid point is also called an edge, and it also has a non-negative integer weight (note that a corner grid point may be connected to two additional points at the same time).

Given the color (black or white) of each additional point, you need to color every grid point in the grid either black or white, so that the sum of weights of all edges whose endpoints have different colors is minimized. Output this minimum sum of edge weights.

Input Format

The first line contains three positive integers n,m,Tn, m, T, representing the numbers of horizontal and vertical lines, and the number of queries.

The next n1n - 1 lines each contain mm non-negative integers. The jj-th non-negative integer in the ii-th line x1i,j{x 1}_{i, j} denotes the weight of the edge between (i,j)(i, j) and (i+1,j)(i + 1, j).

The next nn lines each contain m1m - 1 non-negative integers. The jj-th non-negative integer in the ii-th line x2i,j{x 2}_{i, j} denotes the weight of the edge between (i,j)(i, j) and (i,j+1)(i, j + 1).

Then follow TT groups of queries. In the ii-th group, the first line contains a positive integer kik_i, denoting the total number of additional points in this query. The next kik_i lines each contain three non-negative integers. In the jj-th line, x3i,j,pi,j,ti,j{x 3}_{i, j}, p_{i, j}, t_{i, j} denote the weight of the edge between the jj-th additional point and its adjacent grid point, the index of the ray it lies on, and the color of the additional point (00 for white, 11 for black). It is guaranteed that within the same query group, all pi,jp_{i, j} are distinct.

Multiple integers on the same line are separated by spaces.

Output Format

Output TT lines. The ii-th line contains a non-negative integer, denoting the minimum possible sum of weights of edges whose endpoints have different colors after coloring for the ii-th query.

2 3 1
9 4 7
3 8
10 5
2
19 3 1
17 9 0

12

见附件中的 traffic/traffic2.in
见附件中的 traffic/traffic2.ans
见附件中的 traffic/traffic3.in
见附件中的 traffic/traffic3.ans
见附件中的 traffic/traffic4.in
见附件中的 traffic/traffic4.ans
见附件中的 traffic/traffic5.in
见附件中的 traffic/traffic5.ans

Hint

Sample Explanation #1

An optimal solution: (1,3),(1,2),(2,3)(1, 3), (1, 2), (2, 3) are black; (1,1),(2,1),(2,2)(1, 1), (2, 1), (2, 2) are white.

Constraints

Test Point ID n,mn, m \le kik_i \le
121 \sim 2 55 5050
353 \sim 5 1818 22
686 \sim 8 5050
9109 \sim 10 100100 22
111211 \sim 12 5050
131613 \sim 16 500500 22
172017 \sim 20 5050

For all data, 2n,m5002 \le n, m \le 500, 1T501 \le T \le 50, 1kimin{2(n+m),50}1 \le k_i \le \min \{ 2 (n + m), 50 \}, 1i=1Tki501 \le \sum_{i = 1}^{T} k_i \le 50, 0x1060 \le x \le {10}^6, 1p2(n+m)1 \le p \le 2 (n + m), t{0,1}t \in \{ 0, 1 \}.

It is guaranteed that for each i[1,T]i \in [1, T], all pi,jp_{i, j} are distinct.

[Thanks for providing hack testdata]
@_Enthalpy

Translated by ChatGPT 5