[codevs1048]石子归并 区间dp经典
初始化:f[i][i]置成i,其余为0
三重循环用f[i][j]+f[k][k]更新f[i][j]
最后用一个三重循环更新ans数组,记得提前置成最大值
var w:array[0..105]of longint; f,sum:array[0..105,0..105]of longint; i,n,j,x,y,k:longint; function min(x,y:longint):longint; begin if x>y then exit(y) else exit(x); end; begin readln(n); for i:=1 to n do read(w[i]); fillchar(sum,sizeof(sum),0); fillchar(f,sizeof(f),0); for i:=1 to n do for j:=1 to n do for k:=i to j do inc(sum[i,j],w[k]); for i:=n-1 downto 1 do for j:=i+1 to n do begin f[i,j]:=maxlongint; for k:=i to j do f[i,j]:=min(f[i,j],f[i,k]+f[k+1,j]+sum[i,j]) end; writeln(f[1,n]) end.
喜欢就收藏一下,vic私人qq:1064864324,加我一起讨论问题,一起进步^-^