#P81. [CSP-S 2021] Black and white crosses
[CSP-S 2021] Black and white crosses
Person in Charge
Problem Description
Given horizontal lines and vertical lines on a plane, they intersect to form a grid with rows and columns. The intersection of the -th horizontal line from the top and the -th vertical line from the left is called the grid point . A line segment between any two horizontally or vertically adjacent grid points in the grid is called an edge, and each edge has a non-negative integer weight.
We perform queries, each in the following form:
Given (the value of may vary across queries) additional points, each lying on a ray starting outward from the grid boundary. All such rays starting outward from the grid boundary are numbered from to in the order of $\text{upper left} \to \text{upper right} \to \text{lower right} \to \text{lower left} \to \text{upper left}$, as shown in the figure below:
An image is available here. If not visible, download it from the "Attachments" section.
For each query, the rays where different additional points lie are pairwise distinct. The line segment between each additional point and the nearest grid point is also defined as an edge with a non-negative integer weight (note that a grid point at a corner may be connected to two additional points simultaneously).
Each additional point is assigned a color (black or white). You need to color each grid point in the grid either black or white such that the sum of the weights of all edges whose two endpoints have different colors is minimized. Output this minimal sum of weights.
Input Format
The first line contains three positive integers , representing the number of horizontal lines, vertical lines and queries respectively.
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 groups of queries follow in sequence. The start of the -th group of queries is a line with a positive integer , representing the total number of additional points for this query. The next lines each contain three non-negative integers, where the -th line has in turn, denoting the weight of the edge between the -th additional point and its adjacent grid point, the ray number where the additional point lies, and the color of the additional point ( for white, for black). It is guaranteed that all in the same query are distinct.
Multiple integers in each line are separated by spaces.
Output Format
Output lines, where the -th line contains a non-negative integer representing the minimal sum of the weights of edges with different colored endpoints after coloring for the -th query.
Samples
2 3 1
9 4 7
3 8
10 5
2
19 3 1
17 9 0
12
Sample (1) Explanation
The optimal coloring scheme: are black; are white.
Sample (2)
See traffic/traffic2.in and traffic/traffic2.ans in the contestant's directory.
Sample (3)
See traffic/traffic3.in and traffic/traffic3.ans in the contestant's directory.
Sample (4)
See traffic/traffic4.in and traffic/traffic4.ans in the contestant's directory.
Sample (5)
See traffic/traffic5.in and traffic/traffic5.ans in the contestant's directory.
Data Range
| Test Case No. | ||
|---|---|---|
For all test data, , , , , , , .
It is guaranteed that for each , all are distinct.