您的位置:首页 > 产品设计 > UI/UE

uva 348 Optimal Array Multiplication Sequence

2014-07-09 09:13 267 查看

Optimal Array Multiplication Sequence

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



The number of columns in the A array must be the same as the number ofrows 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 multiplicationsrequired to compute the entire
C array (which will have the same numberof rows as A and the same number ofcolumns 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 howto proceed. For example, if
X, Y, and Z are arrays, then tocompute X Y Z we could either compute (X Y)
Zor X (Y Z). Suppose X is a

array,
Y is a

array, and
Z is a

array. Let's look at the number of multiplicationsrequired to compute the product using the two different sequences:

(X Y) Z


multiplications to determine theproduct (X Y), a


array.
Then

multiplications to determinethe final result.
Total multiplications: 4500.

X (Y Z)


multiplications to determine theproduct (Y Z), a


array.
Then

multiplications to determine thefinal result.
Total multiplications: 8750.
Clearly we'll be able to compute (X Y) Z using fewer individualmultiplications.

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

Input

For each array in the multiple sequences of arrays to be multiplied youwill be given only the dimensions of the array. Each sequence will consistof an integer
N which indicates thenumber of arrays to be multiplied, and then N pairs of integers, eachpair giving the number of rows and columns in an array; the order in whichthe dimensions are given is the same asthe order in which the arrays are to
be multiplied. A value of zerofor N indicates the end of the input.
N will be no larger than 10.

Output

Assume the arrays are named

. Your output for each inputcase is to be a line containing a parenthesized expression clearlyindicating
the order in which the arrays are tobe multiplied. Prefix the output for each case with the case number(they are sequentially numbered, starting with 1). Your output shouldstrongly resemble that shown in the samplesshown below. If, by chance, there are multiple
correct sequences, anyof 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,dp[i][j]表示区间i,j内的所有情况最小cost,还是习惯递归写dp,枚举i,j中的分开点使得cost最小,并用数组记录区间ij的中间分开点,最后递归输出,这样容易想,但很多情况下即使加了记忆化也会超时,这是要转化为递推式来解。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#define inf 0x3f3f3f3f
int n;
int a[20],b[20];
int pos[20][20];
int DP(int x,int y){
if(x==y) return 0;
if(x+1==y) return a[x]*b[x]*b[y];
int sum = inf;
for(int i=x;i<y;i++){
int p = DP(x,i) , q = DP(i+1,y);
if( sum > p + q + a[x]*b[i]*b[y]){
sum = p + q + a[x]*b[i]*b[y];
pos[x][y] = i;
}
}
return sum;
}
void print(int x,int y){
if(x==y) {
printf("A%d",x+1);
return;
}
printf("(");
print(x,pos[x][y]);
printf(" x ");
print(pos[x][y]+1,y);
printf(")");
}
int main(){
int cas = 1;
while(cin >>n){
if(n==0) break;
for(int i=0;i<n;i++) cin >>a[i] >>b[i];
for(int i=0;i<n;i++){
pos[i][i+1] = i;
}
DP(0,n-1);
/*
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(pos[i][j]!=-1) printf("i:%d j: %d %d\n",i,j,pos[i][j]);
}
}
*/
printf("Case %d: ",cas++);
print(0,n-1);
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: