jainbot27

moortal combat, summits problem <3

Jul 21st, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target("sse4")
  3. #include <bits/stdc++.h>
  4. #define f first
  5. #define s second
  6. #define pb push_back
  7. #define mp make_pair
  8. #define ts to_string
  9. #define ub upper_bound
  10. #define lb lower_bound
  11. const char nl = '\n';
  12. using namespace std;
  13. using ll = long long;
  14. using vi = vector<int>;
  15. using vl = vector<ll>;
  16. using pii = pair<int, int>;
  17. using pll = pair<ll, ll>;
  18. using str = string;
  19. //The following debugging code was taken from Benjamin Qi's template :)
  20. str ts(char c) { return str(1,c); }
  21. str ts(bool b) { return b ? "true" : "false"; }
  22. str ts(const char* s) { return (str)s; }
  23. str ts(str s) { return s; }
  24. template<class A> str ts(complex<A> c) {
  25.     stringstream ss; ss << c; return ss.str(); }
  26. str ts(vector<bool> v) {
  27.     str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
  28.     res += "}"; return res; }
  29. template<size_t SZ> str ts(bitset<SZ> b) {
  30.     str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
  31.     return res; }
  32. template<class A, class B> str ts(pair<A,B> p);
  33. template<class T> str ts(T v) {
  34.     bool fst = 1; str res = "{";
  35.     for (const auto& x: v) {
  36.         if (!fst) res += ", ";
  37.         fst = 0; res += ts(x);
  38.     }
  39.     res += "}"; return res;
  40. }
  41. template<class A, class B> str ts(pair<A,B> p) {
  42.     return "("+ts(p.f)+", "+ts(p.s)+")"; }
  43. void DBG() { cerr << "]" << endl; }
  44. template<class H, class... T> void DBG(H h, T... t) {
  45.     cerr << ts(h); if (sizeof...(t)) cerr << ", ";
  46.     DBG(t...); }
  47.  
  48. #ifdef LOCAL
  49. #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  50. #else
  51. #define dbg(...) 0
  52. #endif
  53. //CODE STARTS HERE-----------------------------------------------------------------------------------------//
  54. const int N = 1e5 + 10;
  55. int n, m, k;
  56. int d[26][26];
  57. int dp[N][26];
  58. int p[N][26];
  59. string c;
  60. signed main(){
  61.     cin.tie(nullptr);
  62.     ios_base::sync_with_stdio(false);
  63.     freopen("cowmbat.in", "r", stdin);
  64.     freopen("cowmbat.out", "w", stdout);
  65.     cin >> n >> m >> k >> c;
  66.     for(int i = 0; i < m; i++){
  67.         for(int j = 0; j < m; j++){
  68.             cin >> d[i][j];
  69.         }
  70.     }
  71.     bool flag = 1;
  72.     while(flag){
  73.         flag = 0;
  74.         for(int i = 0; i < m; i++){
  75.             for(int j = 0; j < m; j++){
  76.                 for(int k = 0; k < m; k++){
  77.                     if(d[i][j] > d[i][k] + d[k][j]){
  78.                         d[i][j] = d[i][k] + d[k][j];
  79.                         flag=1;
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.     }
  85.     // for(int i =0; i < m; i++) for(int j = 0; j < m; j++) dbg(d[i][j], i, j);
  86.     for(int i =1 ; i <=  n; i++){
  87.         for(int j = 0; j < m; j++){
  88.             p[i][j] = d[c[i-1] - 'a'][j];
  89.             if(i) p[i][j] += p[i-1][j];
  90.             // dbg(p[i][j], i, j);
  91.         }
  92.     }
  93.     //o(26 * 1e5 * 26)
  94.     memset(dp, 0x3f, sizeof(dp));
  95.     for(int i=0; i < m; i++) dp[0][i] = 0;
  96.     // for(int i=0; i < m; i++) dp[1][i] = p[1][i];
  97.     for(int i = 1; i <= n; i++){
  98.         if(i >= k){
  99.             for(int j =0 ; j < m;j++){
  100.                 for(int a = 0; a < m; a++){
  101.                     dp[i][j] = min(dp[i][j] , dp[i-k][a] + p[i][j] - p[i-k][j]);
  102.                 }
  103.             }
  104.         }
  105.         if(i > 1){
  106.             for(int j = 0; j < m; j++){
  107.                 dp[i][j] = min(dp[i][j], dp[i-1][j] + p[i][j] - p[i-1][j]);
  108.             }
  109.         }
  110.         // for(int j = 0; j < m; j++) dbg(dp[i][j],i,j);
  111.     }
  112.     int ans = 2e9;
  113.     for(int i=0 ; i < m; i++){
  114.         ans = min(ans, dp[n][i]);
  115.     }
  116.     cout << ans << nl;
  117. }
Add Comment
Please, Sign In to add comment