Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize ("O3")
- #pragma GCC target("sse4")
- #include <bits/stdc++.h>
- #define f first
- #define s second
- #define pb push_back
- #define mp make_pair
- #define ts to_string
- #define ub upper_bound
- #define lb lower_bound
- const char nl = '\n';
- using namespace std;
- using ll = long long;
- using vi = vector<int>;
- using vl = vector<ll>;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using str = string;
- //The following debugging code was taken from Benjamin Qi's template :)
- str ts(char c) { return str(1,c); }
- str ts(bool b) { return b ? "true" : "false"; }
- str ts(const char* s) { return (str)s; }
- str ts(str s) { return s; }
- template<class A> str ts(complex<A> c) {
- stringstream ss; ss << c; return ss.str(); }
- str ts(vector<bool> v) {
- str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
- res += "}"; return res; }
- template<size_t SZ> str ts(bitset<SZ> b) {
- str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
- return res; }
- template<class A, class B> str ts(pair<A,B> p);
- template<class T> str ts(T v) {
- bool fst = 1; str res = "{";
- for (const auto& x: v) {
- if (!fst) res += ", ";
- fst = 0; res += ts(x);
- }
- res += "}"; return res;
- }
- template<class A, class B> str ts(pair<A,B> p) {
- return "("+ts(p.f)+", "+ts(p.s)+")"; }
- void DBG() { cerr << "]" << endl; }
- template<class H, class... T> void DBG(H h, T... t) {
- cerr << ts(h); if (sizeof...(t)) cerr << ", ";
- DBG(t...); }
- #ifdef LOCAL
- #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
- #else
- #define dbg(...) 0
- #endif
- //CODE STARTS HERE-----------------------------------------------------------------------------------------//
- const int N = 1e5 + 10;
- int n, m, k;
- int d[26][26];
- int dp[N][26];
- int p[N][26];
- string c;
- signed main(){
- cin.tie(nullptr);
- ios_base::sync_with_stdio(false);
- freopen("cowmbat.in", "r", stdin);
- freopen("cowmbat.out", "w", stdout);
- cin >> n >> m >> k >> c;
- for(int i = 0; i < m; i++){
- for(int j = 0; j < m; j++){
- cin >> d[i][j];
- }
- }
- bool flag = 1;
- while(flag){
- flag = 0;
- for(int i = 0; i < m; i++){
- for(int j = 0; j < m; j++){
- for(int k = 0; k < m; k++){
- if(d[i][j] > d[i][k] + d[k][j]){
- d[i][j] = d[i][k] + d[k][j];
- flag=1;
- }
- }
- }
- }
- }
- // for(int i =0; i < m; i++) for(int j = 0; j < m; j++) dbg(d[i][j], i, j);
- for(int i =1 ; i <= n; i++){
- for(int j = 0; j < m; j++){
- p[i][j] = d[c[i-1] - 'a'][j];
- if(i) p[i][j] += p[i-1][j];
- // dbg(p[i][j], i, j);
- }
- }
- //o(26 * 1e5 * 26)
- memset(dp, 0x3f, sizeof(dp));
- for(int i=0; i < m; i++) dp[0][i] = 0;
- // for(int i=0; i < m; i++) dp[1][i] = p[1][i];
- for(int i = 1; i <= n; i++){
- if(i >= k){
- for(int j =0 ; j < m;j++){
- for(int a = 0; a < m; a++){
- dp[i][j] = min(dp[i][j] , dp[i-k][a] + p[i][j] - p[i-k][j]);
- }
- }
- }
- if(i > 1){
- for(int j = 0; j < m; j++){
- dp[i][j] = min(dp[i][j], dp[i-1][j] + p[i][j] - p[i-1][j]);
- }
- }
- // for(int j = 0; j < m; j++) dbg(dp[i][j],i,j);
- }
- int ans = 2e9;
- for(int i=0 ; i < m; i++){
- ans = min(ans, dp[n][i]);
- }
- cout << ans << nl;
- }
Add Comment
Please, Sign In to add comment