Optimal Array Multiplication Sequence
Time limit:3000ms Memory limit:0kb 64bit IO format:%lld &%llu
Submit Status Practice UVA 348
Appoint Description:
Description
Download as PDF
Given Arrays A and B, we can determine the array C = AB using the standard definition of the matrix multiplication:
The
The number of columns in the A array must is the same as the number of the rows in the B array. Notationally, let's say that rows (a) and columns (a) is the number of rows and columns, respectively, in the A array. The number of individual multiplications required to compute the entire C array (which would have the same number of rows a S A and the same number of columns as B) are then rows (a) columns (B) columns (a). For example, if a was a tex2html_wrap_inline67 array, and B is a tex2html_wrap_inline71 array, it would take tex2html_wrap_i Nline73, or multiplications to compute the C array.
To perform multiplication of more than and arrays we have a choice of what to proceed. For example, if X, Y, and Z is arrays, then to compute XYZ we could either compute (XY) Z or X (YZ). Suppose X is a tex2html_wrap_inline103 array, Y are a tex2html_wrap_inline67 array, and Z is a tex2html_wrap_inline111 arra Y. Let's look at the number of multiplications required to compute the product using the and the both different sequences:
(XY) Z
X (YZ)
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 is to determine an optimal computational seque nCE. Optimality, for this problem, was relative to the number of individual multiplications required.
Input
For each array in the multiple sequences of arrays to is multiplied you'll be given only the dimensions of the array. Each sequence would consist of an integer n which indicates the number of arrays to being multiplied, and then N pairs of inte Gers, each pair giving the number of rows and columns in an array; The order in which the dimensions was given is the same as the order in which the arrays was to be multiplied. A value of zero for N indicates the end of the input. N would be no larger than 10.
Output
Assume the arrays is named tex2html_wrap_inline157. Your output for each input case was to was a line containing a parenthesized expression clearly indicating the order in whic H The arrays is to be multiplied. Prefix the output for each case with the case number (they is sequentially numbered, starting with 1). Your output should strongly resemble that shown in the samples shown below. If, by chance, there is multiple correct sequences, any of these would 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))
//optimal matrix chain multiplication, path output//Combined with the Rujia book, the harvest is the output of the path//dp[i][j]=max{dp[i][k]+dp[k+1][j]+a (i-1) *a (k) *a (j)}#include <cstdio>#include <cstring>using namespace STD;Const intmaxn= the;Const Long Longinf=9999999999;intNintDP[MAXN][MAXN];intP[MAXN][MAXN];intLEFT[MAXN],RIGHT[MAXN];intCasesvoidPrintintXintY) {//Recursively return to the output of the path if(x==y) {printf("a%d", x+1);return; }printf("("); Print (X,p[x][y]);//i-->k printf("x"); Print (p[x][y]+1, y);//k+1--->j printf(")");}voidSolve () { for(intI=0; i<n;i++) { for(intj=i;j<n;j++) {if(I==J) dp[i][j]=0;ElseDp[i][j]=inf; } }memset(P,0,sizeof(p)); for(intD=1;d <n;d++) { for(intI=0; i+d<n;i++) {intJ=i+d; for(intk=i;k<j;k++) {inttmp=dp[i][k]+dp[k+1][J]+LEFT[I]*RIGHT[K]*RIGHT[J];//printf ("tmp%d\n", TMP); if(dp[i][j]>tmp) {dp[i][j]=tmp; P[i][j]=k; } } } }printf("Case%d:", cases++); Print0, N-1);puts("");}intMain () {cases=1; while(~scanf("%d", &n) &&n) { for(intI=0; i<n;i++) {scanf("%d%d", &left[i],&right[i]); } solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Uva348optimal Array multiplication Sequence (optimal matrix chain multiplication + path Output)