#FT0000097. Các tấm ván bật (Springboards)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Các tấm ván bật (Springboards)
Springboards
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
Bessie starts at on a square grid and wants to reach . She may walk only right (increasing ) or up (increasing ), never left or down. Walking distance is the sum of the lengths of her walking segments.
There are springboards. Springboard starts at and ends at . Upon reaching its starting point, Bessie may instantly travel to the endpoint at zero walking cost. Every springboard moves neither left nor down: and . She may use or ignore each board.
All supplied springboard start and end locations are distinct. Find the minimum total walking distance required to reach , excluding all distance traveled using springboards.
Input
The first line contains . Each of the next lines contains four integers for one springboard.
Output
Print one integer: the minimum walking distance from to .
Subtasks
- Subtask 1 (20 points): ; ; ; ; all springboard endpoint coordinates are pairwise distinct.
- Subtask 2 (30 points): ; ; ; ; all springboard endpoint coordinates are pairwise distinct.
- Subtask 3 (50 points): ; ; ; ; all springboard endpoint coordinates are pairwise distinct.
Examples
Example 1
Input
3 2
0 1 0 2
1 2 2 3
Output
3
Explanation
Bessie walks from to (cost ), uses the first board to reach for free, walks to (another ), uses the second board to reach for free, and walks to (another ). Total walking cost: . This is optimal because the two boards cannot save more than their fixed displacement, totaling units.
Example 2
Input
2 1
0 0 2 2
Output
0
Explanation
The springboard starts at and ends at the target . Bessie uses it immediately without walking, so the answer is , which is the minimum possible.