|
| 1 | +/* |
| 2 | +Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications. There are many options to multiply a chain of matrices because matrix multiplication is associative i.e. no matter how one parenthesize the product, the result will be the same. |
| 3 | +
|
| 4 | +Example: |
| 5 | + if you had four matrices A, B, C, and D, you would have: |
| 6 | +
|
| 7 | + (ABC)D = (AB)(CD) = A(BCD) = .... |
| 8 | +However, the order in which one parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency. |
| 9 | +
|
| 10 | +For example: |
| 11 | +
|
| 12 | +A: 10 × 30 matrix |
| 13 | +B : 30 × 5 matrix |
| 14 | +C : 5 × 60 matrix |
| 15 | +Then, |
| 16 | + (AB)C = (10×30×5) + (10×5×60) |
| 17 | + = 1500 + 3000 |
| 18 | + = 4500 operations |
| 19 | + A(BC) = (30×5×60) + (10×30×60) |
| 20 | + = 9000 + 18000 |
| 21 | + = 27000 operations. |
| 22 | +Given an array arr[] which represents the chain of matrices such that the ith matrix Ai is of dimension arr[i-1] x arr[i]. Your task is to write a function that should print the minimum number of multiplications needed to multiply the chain. |
| 23 | +
|
| 24 | + Input: p[] = {40, 20, 30, 10, 30} |
| 25 | + Output: 26000 |
| 26 | + There are 4 matrices of dimensions 40x20, |
| 27 | + 20x30, 30x10 and 10x30. Let the input 4 |
| 28 | + matrices be A, B, C and D. The minimum |
| 29 | + number of multiplications are obtained |
| 30 | + by putting parenthesis in following way |
| 31 | + (A(BC))D --> 20*30*10 + 40*20*10 + 40*10*30 |
| 32 | +
|
| 33 | + Input: p[] = {10, 20, 30, 40, 30} |
| 34 | + Output: 30000 |
| 35 | + There are 4 matrices of dimensions 10x20, |
| 36 | + 20x30, 30x40 and 40x30. Let the input 4 |
| 37 | + matrices be A, B, C and D. The minimum |
| 38 | + number of multiplications are obtained by |
| 39 | + putting parenthesis in following way |
| 40 | + ((AB)C)D --> 10*20*30 + 10*30*40 + 10*40*30 |
| 41 | +Input: |
| 42 | +The first line of the input contains an integer T, denoting the number of test cases. Then T test case follows. The first line of each test case contains an integer N, denoting the number of elements in the array. |
| 43 | +Then next line contains N space separated integers denoting the values of the element in the array. |
| 44 | +
|
| 45 | +Output: |
| 46 | +For each test case the print the minimum number of operations needed to multiply the chain. |
| 47 | +
|
| 48 | +Constraints: |
| 49 | +1<=T<=100 |
| 50 | +2<=N<=100 |
| 51 | +1<=A[]<=500 |
| 52 | +
|
| 53 | +Example: |
| 54 | +Input: |
| 55 | +2 |
| 56 | +5 |
| 57 | +1 2 3 4 5 |
| 58 | +3 |
| 59 | +3 3 3 |
| 60 | +Output: |
| 61 | +38 |
| 62 | +27 |
| 63 | +*/ |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +#include<bits/stdc++.h> |
| 70 | +using namespace std; |
| 71 | + |
| 72 | +int dp[102][102]; |
| 73 | + |
| 74 | +int MCM(int a[], int i, int j){ |
| 75 | + if(i>=j) return 0; |
| 76 | + if(dp[i][j]!=-1) return dp[i][j]; |
| 77 | + int mini=INT_MAX; |
| 78 | + for(int k=i;k<j;k++){ |
| 79 | + int temp=MCM(a,i,k)+MCM(a,k+1,j)+(a[i-1]*a[k]*a[j]); |
| 80 | + if(temp<mini) mini=temp; |
| 81 | + } |
| 82 | + return dp[i][j]=mini; |
| 83 | +} |
| 84 | + |
| 85 | +int main(){ |
| 86 | + ios_base::sync_with_stdio(false); |
| 87 | + cin.tie(NULL); |
| 88 | + cout.tie(NULL); |
| 89 | + int t; |
| 90 | + cin>>t; |
| 91 | + while(t--){ |
| 92 | + int n; |
| 93 | + cin>>n; |
| 94 | + int a[n]; |
| 95 | + for(int i=0;i<n;i++) cin>>a[i]; |
| 96 | + memset(dp, -1, sizeof(dp)); |
| 97 | + cout<<MCM(a,1,n-1)<<endl; |
| 98 | + } |
| 99 | + |
| 100 | +return 0; |
| 101 | +} |
0 commit comments