-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrixChainMultiply.cpp
76 lines (69 loc) · 1.43 KB
/
MatrixChainMultiply.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
67
68
69
70
71
72
73
74
75
76
//
// Created by 86184 on 2021-06-22.
//
#include <iostream>
using namespace std;
struct TA
{
long m;//最小乘法工作量
int k;//分割处
TA():k(0),m(0){}//初始化
};
void matrixchain(int num,int r[],TA **A){
for (int l = 1; l <=num; ++l) {
A[l][l].m=0;//可看作第一条对角线
A[l][l].k=l;
}
for (int l = 2; l <=num ; ++l) {//从第二条对角线开始
for (int i = 1; i <=num-l+1; ++i) {
int j=i+(l-1);
int kk=i;
int m=A[i][i].m+A[i+1][j].m+r[i-1]*r[i]*r[j];
//以下求最小值
for (int k=i+1;k<j;k++){
long mm=A[i][k].m+A[k+1][j].m+r[i-1]*r[k]*r[j];
if(mm<m)
{
m=mm;
kk=k;
}
}
A[i][j].m=m;
A[i][j].k=kk;
}
}
}
void traceback(int i,int j,TA **A){
if (i==j)
{
cout<<"A"<<i;
return;
}
cout<<"(";
traceback(i,A[i][j].k,A);
traceback(A[i][j].k+1,j,A);
cout<<")";
}
int main()
{
int n=0;
cout<<"请输入矩阵数量"<<endl;
cin>>n;
TA **A=new TA*[n+1];
for (int i = 0; i <n ; ++i) {
A[i]= new TA[n+1];
}
int *r=new int[n+1];
cout<<"请输入"<<n+1<<"个矩阵横纵序列"<<endl;
for(int i=0;i<=n;i++)
{
cin>>r[i];
}
matrixchain(n,r,A);
cout<<A[1][n].m<<endl;
traceback(1,n,A);
}
//测试数据
//
// 6
//30 35 15 5 10 20 25