#P3644. [APIO2015] 巴邻旁之桥
[APIO2015] 巴邻旁之桥
Description
An east–west Mu Xi River divides Palembang City into two parts, region and region .
Exactly buildings are built along each bank. Along each bank, buildings are numbered from to . Every pair of adjacent buildings is unit apart, and the river is unit wide. In region , building is directly opposite building in region .
There are residents in the city. The -th resident’s home is at building in region , and their office is at building in region . A resident’s home and office may lie on different banks of the river, in which case they must take a boat to commute, which many people find inconvenient. To allow residents to drive to work, the government decides to build at most bridges across the river.
Due to technical reasons, each bridge must connect the two banks exactly, be strictly perpendicular to the river, and no two bridges may intersect.
After the government builds at most bridges, let be the shortest driving distance from the -th resident’s home to their office. Help the government build the bridges to minimize .
Input Format
The first line contains two positive integers and , the maximum number of bridges and the number of residents.
Each of the next lines contains four parameters: , , , and , meaning that the -th resident’s home is at building in region , and their office is at building in region .
Output Format
Output a single line containing one integer, the minimum value of .
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
24
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
22
Hint
Constraints
For all testdata, it is guaranteed that and are one of the characters "A" and "B", , and there may be more than home or office (or both) in the same building.
Subtask 1 (8 points)
.
Subtask 2 (14 points)
.
Subtask 3 (9 points)
.
Subtask 4 (32 points)
.
Subtask 5 (37 points)
.
Translated by ChatGPT 5
京公网安备 11011102002149号