Connect Five
Time limit2.5sMemory limit1024 MB
Given five grid points, find the fewest road segments that must be refurbished so every pair is joined by a shortest axis-aligned path.
- Level
Medium7 of 10
- Topics
- Geometry, Brute force, Implementation
- Solved
- No attempts yet
Problem
In the town of Nattanham, all roads run either north to south or east to west, and each one spans the entire town. All roads are an equal distance apart, so navigating the town is easy.
The roads are in poor shape and need a fresh layer of asphalt. There is not enough money to fix all of them, so some sections need to be given priority.
The mayor has picked five important locations: the city hall, the police station, the hospital, the fire department, and the mayor's house. Each location is at an intersection.
For every pair of these locations, the mayor wants a shortest path between them that consists entirely of refurbished road. Within that requirement, he wants to refurbish as little road as possible. Intersections do not count toward the total. Figure C.1 shows an optimal configuration of refurbished roads.

Figure C.1: The locations labelled by their initial letters, and a possible way of refurbishing the minimum number of road segments (22). The point is at the bottom-left corner of the grid.
Input
The input has five lines. Each line contains two integers and (), the grid coordinates of one important location.
The locations are distinct.
Output
Output the minimum number of road segments that need to be refurbished.