#P5839. [USACO19DEC] Moortal Cowmbat G

    ID: 6591 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2019USACO前缀和Floyd 算法

[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 MM keys, labeled as the first MM lowercase letters. Bessie's favorite combo in the game is a key string SS of length NN. 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 KK times. Bessie wants to modify her favorite combo to create a new combo of the same length NN, but this new combo must be composed of such key combos to fit the new rules.

Bessie needs to spend aija_{ij} days to train herself to replace key ii with key jj at a specific position in the combo (that is, the cost to change a specific character in SS from ii to jj is aija_{ij}). Note that it may be faster to change key ii to some intermediate key kk and then change key kk to key jj than to change directly from key ii to key jj (or, more generally, there may be a change path from ii to jj that gives the minimum total cost to finally change ii into jj).

Help Bessie find the minimum number of days needed to create a combo that satisfies the new requirements.

Input Format

The first line contains NN, MM, and KK. The second line contains SS. The last MM lines contain an M×MM\times M matrix of integers aija_{ij}, where aija_{ij} is an integer in the range 010000 \ldots 1000, and for all ii, aii=0a_{ii} = 0.

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 1+4+0+0=51+4+0+0=5 days, and the final combo is bbccc.

Test point properties:

Test points 242\sim 4 satisfy N1000N\le 1000, K50K\le 50.

Test points 585\sim 8 satisfy N3×104N\le 3\times 10^4, K50K\le 50.

For 100%100\% of the data, 1M261 \leq M \leq 26, 1KN1051 \leq K\leq N \leq 10^5.

Problem by: Eric Wei.

Translated by ChatGPT 5