Uva--348(动规)
2014-07-30 12:24:09
Optimal Array Multiplication Sequence |
Given two arrays A and B, we can determine the array C = AB 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) andcolumns(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(A)columns(B) columns(A). For example, if A is a array, and B is a array, it will take , 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 X, Y, and Z are arrays, then to compute XYZ we could either compute (XY) Z or X (YZ). Suppose X is a array, Y is a array, and Z is a array. Let's look at the number of multiplications required to compute the product using the two different sequences:
(XY) Z
- multiplications to determine the product (X Y), a array.
- Then multiplications to determine the final result.
- Total multiplications: 4500.
X (YZ)
- multiplications to determine the product (YZ), a array.
- Then multiplications to determine the final result.
- Total multiplications: 8750.
Clearly we'll be able to compute (XY) Z 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 . 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))
思路:经典dp,最优矩阵链乘。我个人理解为分治贪心法。(其实核心思想就那么几个,有类似性。)通过枚举最后一次乘法的位置就可以分治解决。(具体见小白书和代码)
递归输出的方式是借鉴的,值得看看。
1 /************************************************************************* 2 > File Name: m.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 30 Jul 2014 01:15:36 AM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 struct node{ 17 int r,c; 18 }no[15]; 19 20 int dp[15][15],n,ans[15][15]; 21 22 int Solve(int x,int y){ 23 if(dp[x][y] != -1) 24 return dp[x][y]; 25 ans[x][y] = x; 26 if(y == x) 27 return dp[x][y] = 0; 28 if(y - x == 1) 29 return (no[x].r * no[x].c * no[y].c); 30 int tem,tmin = 1e9; 31 for(int i = x; i < y; ++i){ 32 if(no[i].c != no[i + 1].r) 33 continue; 34 tem = Solve(x,i) + Solve(i + 1,y) + no[x].r * no[i].c * no[y].c; 35 if(tem < tmin){ 36 tmin = tem; 37 ans[x][y] = i; 38 } 39 } 40 return dp[x][y] = tmin; 41 } 42 43 void Print(int x,int y){ 44 if(x > y) 45 return; 46 if(x == y){ 47 printf("A%d",x + 1); 48 return; 49 } 50 printf("("); 51 Print(x,ans[x][y]); 52 printf(" x "); 53 Print(ans[x][y] + 1,y); 54 printf(")"); 55 } 56 57 int main(){ 58 int Case = 0; 59 while(scanf("%d",&n) == 1 && n){ 60 memset(dp,-1,sizeof(dp)); 61 for(int i = 0; i < n; ++i) 62 scanf("%d%d",&no[i].r,&no[i].c); 63 printf("Case %d: ",++Case); 64 Solve(0,n - 1); 65 Print(0,n - 1); 66 printf("\n"); 67 } 68 return 0; 69 }