forked from kt0110/hack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixmultiply.cpp
More file actions
49 lines (40 loc) · 817 Bytes
/
matrixmultiply.cpp
File metadata and controls
49 lines (40 loc) · 817 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
#include <bits/stdc++.h>
#include <climits>
#include <iostream>
using namespace std;
int mcm(int *q, int m)
{
n++;
int m[n][n];
int i, j, k, L, q;
for (i = 0; i < m; i++)
{
m[i][i] = 0;
}
for (L = 2; L < m; L++)
{
for (i = 1; i < m - L + 1; i++)
{
j = i + L - 1;
m[i][j] = INT_MAX;
for (k = i; k <= j - 1; k++)
{
q = m[i][k] + m[k + 1][j] + q[i - 1] * q[k] * q[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
return m[1][m - 1];
}
int main()
{
int m;
cin >> m;
int *q = new int[m];
for (int i = 0; i <= m; i++)
{
cin >> q[i];
}
cout << mcm(q, m);
}