[BOJ 13767] Hiking

View as PDF

Submit solution

Points: 3
Time limit: 10.0s
Memory limit: 512M

Problem types
Allowed languages
Assembly, Awk, C, C++, Java, Pascal, Perl, Python, Sed, Text

Alice likes to hike. She likes some bits of track and dislikes others. A cost function will model her distaste for (or liking of) all available track sections. Given a map with the costs of going from one location to the next, find the average cost for a hike in that area.</p>

The map is a rectangular grid of cells. Each cell has information about the cost of going from it to each of its northern, western, southern and eastern neighbours. A hike is a finite sequence of steps each of which goes from a cell to one of its neighbours. The cost of a hike is the sum of the costs of its steps.

When Alice hikes from a given start point to a given end point, she always chooses a minimal cost hike. Determine the average cost of such minimal-cost hikes taken over all possible start and end cells. The start and end cells of a hike must be different. Each pair of start and end cells counts just once towards the average even if there are several minimal-cost hikes between the two cells.

입력 형식

The input will contain a single test case.</p>

The first line contains two integers w and h specifying the width and the height of the map (2 ≤ w, h ≤ 50). Each of the following h lines contains w integers nij (1 ≤ i ≤ h and 1 ≤ j ≤ w and −107 ≤ nij ≤ 107 ) specifying the cost of going from cell (i, j) one step to its northern neighbour (i − 1, j). Then follow three blocks of h lines each, similarly specifying costs for the western, southern and eastern neighbours in this order.

It is not permitted to walk off the map; the corresponding costs will be 0. A minimal-cost hike will exist between any two cells.

출력 형식

Display the smallest integer that is greater than or equal to the average cost of minimal-cost hikes among all pairs of distinct start and end cells.

예제 입력

2 2
0 0
-200 0
0 100
0 500
400 300
0 0
200 0
-400 0

예제 출력

50

Comments

There are no comments at the moment.