forked from rishabh-bansal/GitStart
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMCM.cpp
More file actions
68 lines (58 loc) · 976 Bytes
/
Copy pathMCM.cpp
File metadata and controls
68 lines (58 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//implementation of matrix chain multiplication (dynamic programming)
#include<bits/stdc++.h>
using namespace std;
int m[100][100];
int s[100][100];
int m_cal(vector<int> p)
{
int n=p.size()-1;
int min;
for(int i=1;i<=n;i++)
{
m[i][i]=0;
}
for(int i=1;i<n;i++)
{
for(int j=1;j+i<=n;j++)
{
min=INT_MAX;
for(int k=j;k<j+i;k++)
{
if(min>m[j][k]+m[k+1][j+i]+p[j-1]*p[k]*p[j+i])
{ min=m[j][j+i]=m[j][k]+m[k+1][j+i]+p[j-1]*p[k]*p[j+i];
s[j][j+i]=k;}
}
//cout<<"m("<<j<<","<<j+i<<")="<<m[j][j+i]<<" ";
}
//cout<<endl;
}
return m[1][n];
}
void print_mcm(int i,int j)
{//cout<<"{"<<i<<","<<j<<"}";
if(i==j)
cout<<"A"<<i;
else
{
cout<<"(";
print_mcm(i,s[i][j]);
print_mcm(s[i][j]+1,j);
cout<<")";
}
}
int main()
{
vector<int> p;
int x;
while(1)
{
cin>>x;
if(x<0)
break;
p.push_back(x);
}
cout<<"no. of multiplications="<<m_cal(p)<<endl;
print_mcm(1,p.size()-1);
cout<<endl;
return 0;
}