#P6158. 封锁
封锁
Background
Shocking news! zbw has actually escaped from the prison in City B!
As the police chief of City B, you must catch zbw before he escapes from your jurisdiction.
Problem Description
City B can be seen as an grid. The prison is at , and the only exit out of City B is at . Between every two adjacent points (where ) there is an undirected road (there are no diagonal roads). You need to deploy guards on some roads so that no matter how zbw moves, he will have to pass through at least one of these guarded roads.
The cost to deploy guards on the road from to is . The cost to deploy guards on the road from to is . Also, deploying guards affects people’s lives. The impact on people’s lives caused by deploying guards on the road from to is , and the impact caused by deploying guards on the road from to is .
Let the total cost be , and the total impact be . As an excellent police chief, you need to minimize .
Input Format
The first line contains an integer .
Then there are lines. In the -th line, there are two integers and .
Then there are lines. In the -th line, there are two integers and .
That is, the information of vertical edges is given first from top to bottom and from left to right, and then the information of horizontal edges is given from top to bottom and from left to right.
If you do not understand, please refer to the sample explanation.
Output Format
Output one integer on a single line, which is the minimum value of .
3
8 3
5 2
1 1
4 2
1 2
7 5
7 2
6 1
5 4
2 3
1 4
4 3
49
Hint

As shown in the figure, the top-left corner is and the bottom-right corner is . The blue numbers represent , the red numbers represent , the yellow numbers represent , and the green numbers represent .
The optimal plan is to guard three edges:
, , .
The edge weights of the three edges are --- --- .
The answer is .
You can see that there is no better solution.
This problem uses bundled testdata.
| Subtasks | Special property | Score | |
|---|---|---|---|
| Subtask 1 | None | ||
| Subtask 2 | Random testdata | ||
| Subtask 3 | None | ||
| Subtask 4 | |||
| Subtask 5 |
Constraints: for all testdata, , $0 \leq r_{i,j}, d_{i,j}, x_{i,j}, y_{i,j} \leq 10^3$.
The testdata was strengthened on 2020/3/4, removing some solutions with incorrect time complexity.
Translated by ChatGPT 5