2018.07.06【2018提高组】模拟C组
题目:矩阵【推荐】 (Standard IO)
N(2<=N<=500)个矩阵相乘,求进行乘法的最少次数,我们认为两个矩阵A(m*n)*B(n*p)的乘法次数为m*n*p次。
数据:
Input
第一行是整数N,接下来N行是对每个矩阵的描述,一行两个整数a,b,(1<=a,b<=50)a表示行,b表示列。输入确保能够相乘。
Output
一行输出最少乘法次数。
Sample Input
3
50 10
10 20
20 5
Sample Output
3500
思路:
记忆化深搜
var
f:array[-2..505,-2..505] of longint;
a:array[-2..505] of longint;
i,j,k,n,m:longint;
function min(a,b:longint):longint;
begin
if a<b then exit(a);
exit(b);
end;
function dfs(x,y:longint):longint;
var
i,ans:longint;
begin
if x=y then exit(0);
if f[x,y]>0 then exit(f[x,y]);
ans:=maxlongint;
for i:=x to y-1 do
ans:=min(ans,dfs(x,i)+dfs(i+1,y)+a[x-1]*a[i]*a[y]);
f[x,y]:=ans;
exit(ans);
end;
begin
readln(n);
for i:=0 to n-1 do
readln(a[i],a[i+1]);
writeln(dfs(1,n));
end.