#P5839. [USACO19DEC] Moortal Cowmbat G
[USACO19DEC] Moortal Cowmbat G
Problem Description
Bessie has been playing the fighting game "Moortal Cowmbat" for a long time. However, the game developers recently released an update that forces Bessie to change her playstyle.
The game uses a total of keys, labeled as the first lowercase letters. Bessie's favorite combo in the game is a key string of length . However, due to the recent update, every combo must be made up of some "combos", where a combo is defined as pressing the same key consecutively at least times. Bessie wants to modify her favorite combo to create a new combo of the same length , but this new combo must be composed of such key combos to fit the new rules.
Bessie needs to spend days to train herself to replace key with key at a specific position in the combo (that is, the cost to change a specific character in from to is ). Note that it may be faster to change key to some intermediate key and then change key to key than to change directly from key to key (or, more generally, there may be a change path from to that gives the minimum total cost to finally change into ).
Help Bessie find the minimum number of days needed to create a combo that satisfies the new requirements.
Input Format
The first line contains , , and . The second line contains . The last lines contain an matrix of integers , where is an integer in the range , and for all , .
Output Format
Output one integer, the minimum number of days needed for Bessie to change her combo into a new combo that satisfies the new requirements.
5 5 2
abcde
0 1 4 4 4
2 0 4 4 4
6 5 0 3 2
5 5 5 0 4
3 7 0 5 0
5
Hint
In this example, the optimal plan is to change a to b, change d to e, and then change both e characters to c. This costs a total of days, and the final combo is bbccc.
Test point properties:
Test points satisfy , .
Test points satisfy , .
For of the data, , .
Problem by: Eric Wei.
Translated by ChatGPT 5