aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTa180m2020-05-01 12:12:14 -0500
committerGitHub2020-05-01 12:12:14 -0500
commit3656c28c77a45d18e5c9542d86e4640f4a374a6d (patch)
treed92050382ad29997081f1a5274b21abe51ea322d
parent0251cb5c553f44c2d431d0050e0990cf573510e2 (diff)
Create 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);
+}