uva 348 Optimal Array Multiplication Sequence

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 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 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 tex2html_wrap_inline103 array, Y is a tex2html_wrap_inline67 array, and Z is a tex2html_wrap_inline111 array. Let's look at the number of multiplicationsrequired to compute the product using the two different sequences:

(X Y) Z

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

X (Y Z)

  • tex2html_wrap_inline133 multiplications to determine theproduct (Y Z), a tex2html_wrap_inline139 array.
  • Then tex2html_wrap_inline141 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 tex2html_wrap_inline157 . 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值