forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_Mutiplication.cpp
66 lines (54 loc) · 894 Bytes
/
Matrix_Mutiplication.cpp
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
#include <bits/stdc++.h>
using namespace std;
int n=0;
int val[100000];
int m=1000000;
int mx[1000][1000];
void print(int x,int y)
{
if(x==y)
{
cout<<x-1;
}
else
{
cout<<"(";
print(x,mx[y][x]);
print(mx[y][x]+1,y);
cout<<")";
}
}
void fun()
{
for(int i=0;i<=n;i++)
mx[i][i]=0;
for(int x=2;x<n;x++)
{
for(int j=1;j<n-x+1;j++)
{
int i=j+x-1;
mx[j][i]=m;
for(int k=j;k<i;k++)
{
int w=mx[j][k]+mx[k+1][i]+val[j-1]*val[k]*val[i];
if(w<mx[j][i])
{
mx[i][j]=k;
mx[j][i]=min(mx[j][i],w);
}
}
}
}
print(1,n-1);
cout<<endl;
cout<<mx[1][n-1]<<endl;
}
int main() {
cin>>n;
for(int i=0;i<n;i++)
{
cin>>val[i];
}
fun();
return 0;
}