luogu#P7916. [CSP-S 2021] 交通规划
[CSP-S 2021] 交通规划
Problem Description
Given horizontal lines and vertical lines on a plane, they intersect to form a grid with rows and columns. The intersection point between the -th horizontal line from top to bottom and the -th vertical line from left to right is called the grid point . 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 queries. Each query is as follows:
You are given (the value of may differ among the 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 to in the order top-left top-right bottom-right bottom-left 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 , representing the numbers of horizontal and vertical lines, and the number of queries.
The next lines each contain non-negative integers. The -th non-negative integer in the -th line denotes the weight of the edge between and .
The next lines each contain non-negative integers. The -th non-negative integer in the -th line denotes the weight of the edge between and .
Then follow groups of queries. In the -th group, the first line contains a positive integer , denoting the total number of additional points in this query. The next lines each contain three non-negative integers. In the -th line, denote the weight of the edge between the -th additional point and its adjacent grid point, the index of the ray it lies on, and the color of the additional point ( for white, for black). It is guaranteed that within the same query group, all are distinct.
Multiple integers on the same line are separated by spaces.
Output Format
Output lines. The -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 -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: are black; are white.
Constraints
| Test Point ID | ||
|---|---|---|
For all data, , , , , , , .
It is guaranteed that for each , all are distinct.
[Thanks for providing hack testdata]
@_Enthalpy。
Translated by ChatGPT 5