Advertisement
vijju525

DAA Implementation Project

May 15th, 2021
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. /*
  2. DAA - IMPLEMENTATION PROJECT
  3. Done By: IMT2019525 VIJAY JAISANKAR, IMT2019049 MADDULA DHANUSH
  4. */
  5.  
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8.  
  9.  
  10. /*
  11.    This is the dp table.
  12.    At the end of the program,dp[i][j] will contain the minimum cost of merging the slimes in the segment i,...,j  
  13. */
  14. long long int dp[401][401];
  15.  
  16.  
  17.  
  18. /*
  19.    This is the array containing partial sum/prefix sum values.
  20.    After preprocessing, pfsum[i] = arr[0] + arr[1] + ... + arr[i],
  21.    (where i is the input array)
  22. */
  23. long long int pfsum[401];
  24.  
  25.  
  26.  
  27. /*
  28.    This is the input array containing the slime values.
  29. */
  30. long long int arr[401];
  31.  
  32.  
  33.  
  34. /*
  35.    A dummy variable used to store the theoretical maximum value possible in this context
  36. */
  37. long long int INF = 1LL<<60;
  38.  
  39.  
  40.  
  41. int main(){
  42.  
  43.     /*
  44.        n is the size of the input.
  45.     */
  46.     int n;
  47.     cin>>n;
  48.    
  49.  
  50.  
  51.     /*
  52.        Constructing the prefix sum array while taking the input:
  53.        We have desctibed this process in the paper - note that this whole process is O(n)
  54.     */
  55.     for(int i=0;i<n;i++){
  56.         cin>>arr[i];
  57.         if(i==0)    pfsum[i] = arr[i];
  58.         else        pfsum[i] = pfsum[i-1] + arr[i];
  59.     }
  60.  
  61.  
  62.  
  63.     /*
  64.        Setting up base cases and initialising the dp[][] array:
  65.        Base cases: dp[i][i] = 0 - this is to facilitate the vacuous step of the recurrence
  66.        Otherwise, as we are going to minimize the dp values, we set them to the aforementioned INF
  67.     */
  68.     for(int i=0;i<n;i++){
  69.         for(int j=0;j<n;j++){
  70.             if(i==j)    dp[i][j] = 0;
  71.             else        dp[i][j] = INF;
  72.         }
  73.     }
  74.    
  75.  
  76.  
  77.     /*
  78.        Implementing the recurrence in a bottom-up fashion:
  79.        From the dependency graph, this is a way to build the DP in a bottom-up way.
  80.        We can clearly see the recurrence in action while setting dp[l][r]
  81.        Note that, as we need the non-recurisive merging work while considering every eligible value of k, we pre-store it.
  82.     */
  83.     for(int l=n-1;l>=0;l--){
  84.         for(int r=l+1;r<n;r++){
  85.             long long int pfdiff = pfsum[r] - pfsum[l-1];
  86.             for(int k=l;k<r;k++){
  87.                 dp[l][r] = min(dp[l][r],dp[l][k] + dp[k+1][r] + pfdiff); // Testing out candidate values of k
  88.             }
  89.         }
  90.     }
  91.  
  92.  
  93.  
  94.     /*
  95.        Outputting the value:
  96.        From the definition of dp[i][j], we need to print out dp[0][n-1] as we follow 0-based indexing.
  97.     */
  98.     cout<<dp[0][n-1]<<"\n";
  99.     return 0;
  100. }      
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement