Advertisement
Guest User

Moortal Cowmbat

a guest
Dec 15th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int OO = 1e9 + 1;
  5.  
  6. int main(){
  7.     freopen("cowmbat.in", "r", stdin);
  8.     freopen("cowmbat.out", "w", stdout);
  9.    
  10.     int nBoutons, nLettres, lenCombo;
  11.     cin >> nBoutons >> nLettres >> lenCombo;
  12.    
  13.     vector<int> comboDep(nBoutons);
  14.     for (int& valeur : comboDep){
  15.         char lettre;
  16.         cin >> lettre;
  17.        
  18.         valeur = lettre - 'a';
  19.     }
  20.    
  21.     int minDelta[nLettres][nLettres];
  22.     for (int iDep = 0; iDep < nLettres; ++iDep)
  23.         for (int iFin = 0; iFin < nLettres; ++iFin)
  24.             cin >> minDelta[iDep][iFin];
  25.            
  26.     //On fait un Floyd-Warshall pour connaitre les vrais min de deltas.
  27.     for (int iTrans = 0; iTrans < nLettres; ++iTrans)
  28.         for (int iDep = 0; iDep < nLettres; ++iDep)
  29.             for (int iFin = 0; iFin < nLettres; ++iFin)
  30.                 minDelta[iDep][iFin] = min(minDelta[iDep][iFin], minDelta[iDep][iTrans] + minDelta[iTrans][iFin]);
  31.    
  32.     /*cout << "----\n";
  33.    
  34.     for (int i = 0; i < nLettres; ++i){
  35.         for (int j = 0; j < nLettres; ++j)
  36.             cout << minDelta[i][j] << " ";
  37.         cout << endl;
  38.     }
  39.     cout << "----\n";*/
  40.     int dp[nBoutons][nLettres][2]; //Dp[i][j] : Min cout pour que ce soit valable de 0 à i inclus où la couleur de i est j.
  41.     int sum[nBoutons][nLettres]; //Sum[i][j] somme de 0 à i inclus des couts pour transformer tout en j
  42.    
  43.     for (int iLettre = 0; iLettre < nLettres; ++iLettre){
  44.         int curValue = 0;
  45.         for (int iFin = 0; iFin < nBoutons; ++iFin){
  46.             curValue += minDelta[comboDep[iFin]][iLettre];
  47.             sum[iFin][iLettre] = curValue;
  48.         }
  49.     }
  50.    
  51.     for (int i = 0; i < nBoutons; ++i)
  52.         for (int j = 0; j < nLettres; ++j)
  53.             dp[i][j][0] = dp[i][j][1] = OO;
  54.    
  55.     for (int iFin = lenCombo - 1; iFin < nBoutons; ++iFin){
  56.         for (int iLettre = 0; iLettre < nLettres; ++iLettre){
  57.             int valMin = -1;
  58.             if (iFin - lenCombo >= lenCombo - 1)
  59.                 valMin = dp[iFin - lenCombo][iLettre][1];
  60.            
  61.             else
  62.                 valMin = 0;
  63.             //valMin = OO;
  64.             dp[iFin][iLettre][0] = valMin + sum[iFin][iLettre];
  65.         /*  valMin = OO;
  66.             for (int i = 0; i <= iFin - lenCombo; ++i)
  67.                 for (int j = 0; j < nLettres; ++j)
  68.                     dp[iFin][iLettre][0] = min(dp[iFin][iLettre][0], dp[i][j][0] + sum[iFin][iLettre] - sum[i][iLettre]);
  69.             for (int j = 0; j < nLettres; ++j)
  70.                 dp[iFin][iLettre][0] = min(dp[iFin][iLettre][0], sum[iFin][iLettre]);*/
  71.         }
  72.        
  73.         /*for (int iLettre = 0; iLettre < nLettres; ++iLettre)
  74.             cout << dp[iFin][iLettre] << "\t";
  75.         cout << endl;
  76.         */
  77.         if (iFin != nBoutons - 1){
  78.             int mini = OO;
  79.             for (int iLettre = 0; iLettre < nLettres; ++iLettre)
  80.                 mini = min(mini, dp[iFin][iLettre][0]);
  81.            
  82.             for (int iLettre = 0; iLettre < nLettres; ++iLettre){
  83.                 dp[iFin][iLettre][1] = mini - sum[iFin][iLettre];
  84.                
  85.                 if (iFin >= lenCombo)
  86.                     //int enlevement = sum[iFin][iLettre] - sum[iFin - 1][iLettre];
  87.                     dp[iFin][iLettre][1] = min(dp[iFin][iLettre][1], dp[iFin - 1][iLettre][1]);
  88.                 dp[iFin][iLettre][1] = min(dp[iFin][iLettre][1], 0);
  89.             }
  90.         }
  91.     }
  92.    
  93.     /*cout << "-\n";
  94.     for (int j = 0; j < nBoutons; ++j){
  95.     for (int i = 0; i < nLettres; ++i)
  96.         cout << dp[j][i] << " ";
  97.     cout << endl;}*/
  98.     int mini = OO;
  99.     for (int iLettre = 0; iLettre < nLettres; ++iLettre)
  100.         mini = min(mini, dp[nBoutons - 1][iLettre][0]);
  101.     cout << mini << endl;
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement