Good Word, Bad Word
Time limit2.5sMemory limit1024 MB
Bacteria on an N by N grid can step to an orthogonal neighbor for cost a, or jump up to Chebyshev distance D from a good cell for cost b; for each meeting cell find the total minimum cost for all bacteria to reach it.
- Level
Hard9 of 10
- Topics
- Shortest path, Graph, BFS, Matrix
- Solved
- No attempts yet
Problem
An onion is planted in each cell of an grid. Each onion is home to one bacterium, and these bacteria hold meetings often. In this grid, the coordinate denotes the cell in the -th row and -th column. Each bacterium has two ways to move: the good word and the bad word. Saying the bad word once consumes energy. To say the good word, the cell the bacterium is currently in must be a good cell, and saying it consumes energy. If the current position is , saying the bad word makes the angry onion shove the bacterium to a neighboring onion, so the bacterium can move to any satisfying ; saying the good word makes the onion grow instantly and toss the bacterium high, so the bacterium can move to any satisfying . Moving outside the grid is of course impossible.
The meeting scheduled for day is held at , and the day's sunlight intensity determines and . Assuming each bacterium moves using the minimum energy, find the total energy the bacteria must spend for all bacteria in the grid to gather. The onions can move only one bacterium at a time. After the meeting ends, every bacterium returns to the onion it lives in, and this costs no energy.
Input
The first line gives the grid size , the movement range of the good word, and the number of meeting days , separated by spaces.
Line gives the state of row of the grid as a string of length . If the -th character is ., then is a bad cell; if it is #, it is a good cell. ()
Line gives the information for the meeting on day : , , , , separated by spaces. ()
Output
On line , print the total energy the bacteria must spend at the meeting on day . ()
Constraints
- ()
- ()