aboutsummaryrefslogtreecommitdiff
path: root/2019/December/Gold/cowmbat.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2019/December/Gold/cowmbat.cpp')
-rw-r--r--2019/December/Gold/cowmbat.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/2019/December/Gold/cowmbat.cpp b/2019/December/Gold/cowmbat.cpp
new file mode 100644
index 0000000..e224936
--- /dev/null
+++ b/2019/December/Gold/cowmbat.cpp
@@ -0,0 +1,48 @@
+#include <bits/stdc++.h>
+using namespace std;
+typedef long long ll;
+
+ll a[30][30], cost[100001][30] = { 0 }, DP[100001][30];
+
+int main() {
+ ifstream cin("cowmbat.in");
+ ofstream cout("cowmbat.out");
+
+ int N, M, K;
+ string S;
+ cin >> N >> M >> K >> S;
+ for (int i = 0; i <= 26; ++i) {
+ for (int j = 0; j <= 26; ++j) a[i][j] = 1e9;
+ }
+ for (int i = 0; i < M; ++i) {
+ for (int j = 0; j < M; ++j) cin >> a[i][j];
+ }
+
+ for (int k = 0; k < M; ++k) {
+ for (int i = 0; i < M; ++i) {
+ for (int j = 0; j < M; ++j) a[i][j] = min(a[i][k] + a[k][j], a[i][j]);
+ }
+ }
+
+ for (int i = 0; i < K; ++i) {
+ for (int j = 0; j <= 26; ++j) cost[0][j] += a[S[i] - 'a'][j];
+ }
+ for (int i = 0; i < N - K; ++i) {
+ for (int j = 0; j <= 26; ++j) {
+ cost[i + 1][j] = a[S[i + K] - 'a'][j] - a[S[i] - 'a'][j] + cost[i][j];
+ }
+ }
+
+ for (int i = 0; i <= N; ++i) {
+ for (int j = 0; j <= 26; ++j) DP[i][j] = 1e9;
+ }
+ DP[0][26] = 0;
+ for (int i = 0; i < N; ++i) {
+ for (int j = 0; j <= 26; ++j) DP[i + 1][j] = min(a[S[i] - 'a'][j] + DP[i][j], DP[i + 1][j]);
+ int best = *min_element(DP[i], DP[i] + 27);
+ if (i + K <= N) {
+ for (int j = 0; j <= 26; ++j) DP[i + K][j] = min(best + cost[i][j], DP[i + K][j]);
+ }
+ }
+ cout << *min_element(DP[N], DP[N] + 27);
+}