Draw in Straight Lines
Time limit3sMemory limit512 MB
Find the minimum cost to paint a black-and-white grid using horizontal or vertical segments (cost a per pixel plus b) and single pixels (cost c).
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming
- Solved
- No attempts yet
Problem
You plan to draw a black-and-white painting on a rectangular canvas. The painting is a grid of pixels, each either black or white. You can paint black or white lines or dots on the initially white canvas.
You can apply a sequence of the following two operations in any order.
- Paint pixels on a horizontal or vertical line segment, one pixel wide and two or more pixels long, either black or white. This operation costs , where is the number of pixels in the segment.
- Paint a single pixel, either black or white. This operation has a constant cost of .
You can overpaint already painted pixels as long as the following conditions hold.
- A pixel can have been painted at most once before. Overpainting a pixel too many times makes the ink layers too thick and the picture ugly. Painting a pixel with the same color also counts as overpainting. For example, if you painted a pixel black twice, you can no longer paint it black or white.
- A pixel painted white cannot be overpainted with black. White ink takes a very long time to dry, so black over white would look gray instead of black. Painting white over black is fine.
Your task is to compute the minimum total cost to draw the specified image.
Input
The first line contains five integers , , , , and . () and () are the height and width of the canvas in pixels. (), (), and () define the painting costs. A line segment of length costs , and a single pixel costs . These constants satisfy .
The next lines each contain a string of length . The -th character of the -th string is "#" if the pixel in row , column should be black, and "." if it should be white.
Output
Output the minimum cost.