#P3756. [CQOI2017] 老C的方块

    ID: 1342 远端评测题 1000~2000ms 125MiB 尝试: 5 已通过: 0 难度: 9 上传者: 标签>2017重庆各省省选网络流图的建立,建图最小割

[CQOI2017] 老C的方块

Description

Old C (Lao C) is a programmer.

As a lazy programmer, Old C often plays a block game on his computer to kill time. The game is played on a grid with RR rows and CC columns of unit cells. Two cells are called adjacent if they share a side, and among adjacent pairs, some shared edges are marked as special. These special edges follow a very regular pattern. First, in row 11, the edge between the first two cells is a special edge. Then, the special edges have period 44 in the horizontal direction and period 22 in the vertical direction. Between every odd-numbered column and the next column there are special edges, and the parity of the rows carrying these edges alternates from left to right.

The figure below shows a grid with R=C=8R = C = 8, where the special edges are marked in blue. First, in row 11, the edge between column 11 and column 22 is special. Because the vertical period is 22, the edge between column 11 and column 22 is special in all odd-numbered rows. Because the horizontal period is 44, the edge between column 55 and column 66 is also special in all odd-numbered rows; if the grid is large enough, so are the edges between columns 99 and 1010, 1313 and 1414, etc. Because every odd-numbered column and the next column have special edges, the edges between columns 33 and 44, and between 77 and 88, are also special; since the row parity alternates from left to right, these special edges lie in even-numbered rows. If the grid is larger, we can find all special edges in the same way.

Each cell can hold exactly one block. At the beginning of the game, some cells already contain blocks, while others are empty. Old C hates the pattern shown below. If he finds some blocks arranged in that hated shape (the positions of the special edges must also match those in the figure), he will immediately rage quit. Even if the hated shape appears after any number of rotations or reflections, he will still rage quit.

To avoid rage quitting, Old C decides to remove some blocks while he still can, so that the remaining blocks cannot form the hated shape. However, removing a block costs some coins, and the cost may differ from block to block. Of course, Old C wants to spend as few coins as possible. What is the minimum total number of coins needed? Old C is too lazy to think about it and leaves the problem to you.

Input Format

The first line contains three positive integers C,R,nC, R, n, meaning that in a grid with CC columns and RR rows, there are nn cells that contain blocks.

Each of the next nn lines contains three positive integers x,y,wx, y, w, meaning that the cell at column xx, row yy contains a block, and removing it costs ww coins. All entries are distinct and within the grid.

Output Format

Output one line containing a single integer, the minimum total number of coins required.

2 2 4
1 1 5 
1 2 6 
2 1 7 
2 2 8 
5
3 3 7 
1 1 10 
1 2 15 
1 3 10 
2 1 10 
2 2 10 
2 3 10 
3 1 10 
15

Hint

[Explanation for Sample 2] As shown in the figure. It is easy to see that if we do not remove the block at column 1, row 2, then we must remove at least two blocks to avoid containing the hated shape, costing at least 20 coins; whereas after deleting the block at column 1, row 2, all existing hated shapes disappear, and we only need to spend 15 coins.

Constraints

For test points 121 \sim 2, 1C,R1001 \leq C, R \leq 100, 1n201 \leq n \leq 20.

For test points 363 \sim 6, 1C,R1051 \leq C, R \leq 10^5, 2000n50002000 \leq n \leq 5000, with graded testdata.

For test points 7107 \sim 10, 1C,R1051 \leq C, R \leq 10^5, 30000n10530000 \leq n \leq 10^5, with graded testdata.

For all test points, 1C,R,n1051 \leq C, R, n \leq 10^5, 1w1041 \leq w \leq 10^4.

Translated by ChatGPT 5