UVa 348 Optimal Array Multiplication Sequence

 Optimal Array Multiplication Sequence 

Given two arrays A and B, we can determine the array C = A B using the standard definition of matrix multiplication:

The number of columns in the A array must be the same as the number of rows in the B array. Notationally, let's say that rows(A) and columns(A) are the number of rows and columns, respectively, in the A array. The number of individual multiplications required to compute the entire C array (which will have the same number of rows as A and the same number of columns as B) is then rows(Acolumns(Bcolumns(A). For example, if Ais a tex2html_wrap_inline67 array, and B is a tex2html_wrap_inline71 array, it will take tex2html_wrap_inline73 , or 3000 multiplications to compute the C array.

To perform multiplication of more than two arrays we have a choice of how to proceed. For example, if XY, and Z are arrays, then to compute X Y Z we could either compute (X YZ or X (Y Z). Suppose X is a tex2html_wrap_inline103array, Y is a tex2html_wrap_inline67 array, and Z is a tex2html_wrap_inline111 array. Let's look at the number of multiplications required to compute the product using the two different sequences:

(X YZ

  • tex2html_wrap_inline119 multiplications to determine the product (X Y), a tex2html_wrap_inline123 array.
  • Then tex2html_wrap_inline125 multiplications to determine the final result.
  • Total multiplications: 4500.

X (Y Z)

  • tex2html_wrap_inline133 multiplications to determine the product (Y Z), a tex2html_wrap_inline139 array.
  • Then tex2html_wrap_inline141 multiplications to determine the final result.
  • Total multiplications: 8750.

Clearly we'll be able to compute (X YZ using fewer individual multiplications.

Given the size of each array in a sequence of arrays to be multiplied, you are to determine an optimal computational sequence. Optimality, for this problem, is relative to the number of individual multiplications required.

Input

For each array in the multiple sequences of arrays to be multiplied you will be given only the dimensions of the array. Each sequence will consist of an integer N which indicates the number of arrays to be multiplied, and then N pairs of integers, each pair giving the number of rows and columns in an array; the order in which the dimensions are given is the same as the order in which the arrays are to be multiplied. A value of zero for N indicates the end of the input. N will be no larger than 10.

Output

Assume the arrays are named tex2html_wrap_inline157 . Your output for each input case is to be a line containing a parenthesized expression clearly indicating the order in which the arrays are to be multiplied. Prefix the output for each case with the case number (they are sequentially numbered, starting with 1). Your output should strongly resemble that shown in the samples shown below. If, by chance, there are multiple correct sequences, any of these will be accepted as a valid answer.

Sample Input

3
1 5
5 20
20 1
3
5 10
10 20
20 35
6
30 35
35 15
15 5
5 10
10 20
20 25
0

Sample Output

Case 1: (A1 x (A2 x A3))
Case 2: ((A1 x A2) x A3)
Case 3: ((A1 x (A2 x A3)) x ((A4 x A5) x A6))

给一些矩阵,求这些矩阵相乘所需的最少乘法次数。

设每个矩阵的行数保存在matrix_a[]中,列数保存在matrix_b[]中

动态规划,设dp[i][j]表示从第i个矩阵乘到第j个矩阵所需的的最少乘法次数

状态转移方程:dp[i][j]=min{ dp[i][k]+dp[k+1][j]+matrix_a[i]*matrix_b[k]*matrix_b[j] | i≤k≤j }

此题难点在于最终输出时的括号和格式,我的做法是,在动态规划的过程中先记录分割点,比如dp[i][j]是由dp[i][k]和dp[k+1][j]转移过来的,那么就令pos[i][j]=k。在动态规划结束后进行一次dfs,统计出各个矩阵左右的括号数。输出时依次输出各个矩阵左边的左括号数、矩阵Ai、右边的右括号数即可,中间用" x "间隔。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define INF 0x7fffffffffffffff
 5 
 6 using namespace std;
 7 
 8 int n;
 9 int pos[15][15];
10 int left_pare[15],right_pare[15];
11 long long matrix_a[15],matrix_b[15];
12 long long dp[15][15];
13 
14 void dfs(int x,int y)
15 {
16     if(x==y)
17         return ;
18 
19     left_pare[x]++;
20     right_pare[y]++;
21 
22     if(x==y-1)
23         return ;
24 
25     dfs(x,pos[x][y]);
26     dfs(pos[x][y]+1,y);
27 }
28 
29 
30 int main()
31 {
32     int kase=0;
33 
34     while(scanf("%d",&n)==1&&n)
35     {
36         for(int i=1;i<=n;i++)
37             scanf("%lld %lld",&matrix_a[i],&matrix_b[i]);
38 
39         kase++;
40         memset(dp,0,sizeof(dp));
41         memset(pos,-1,sizeof(pos));
42 
43         for(int i=1;i<n;i++)
44             dp[i][i+1]=matrix_a[i]*matrix_a[i+1]*matrix_b[i+1];
45         for(int j=2;j<n;j++)
46             for(int i=1;i<=n-j;i++)
47             {
48                 long long Min=INF;
49                 for(int k=0;k<j;k++)
50                 {
51                     long long temp=dp[i][i+k]+dp[i+k+1][i+j]+matrix_a[i]*matrix_b[i+k]*matrix_b[i+j];
52                     if(temp<Min)
53                     {
54                         Min=temp;
55                         pos[i][i+j]=i+k;
56                     }
57                 }
58                 dp[i][i+j]=Min;
59             }
60 
61         memset(left_pare,0,sizeof(left_pare));
62         memset(right_pare,0,sizeof(right_pare));
63 
64         dfs(1,n);
65 
66         printf("Case %d: ",kase);
67         for(int i=1;i<=n;i++)
68         {
69             for(int j=1;j<=left_pare[i];j++)
70                 putchar('(');
71             printf("A%d",i);
72             for(int j=1;j<=right_pare[i];j++)
73                 putchar(')');
74             if(i!=n)
75                 printf(" x ");
76         }
77         printf("
");
78     }
79 
80     return 0;
81 }
[C++]
原文地址:https://www.cnblogs.com/lzj-0218/p/3565855.html