codevs1048

题目地址:http://codevs.cn/problem/1048/

分析:

动态规划

代码:

var

 i,j,k,n:longint;

 x,a:array[0..100,0..100] of longint;


  function min(a,b:longint):longint;

  begin

   if a>b then exit(b);

   exit(a);

  end;


begin

 readln(n);

 for i:=1 to n do read(a[i,i]);

 for i:=1 to n do

 for j:=i+1 to n do

 for k:=i to j do a[i,j]:=a[i,j]+a[k,k];

 for i:=1 to n do

 for j:=1 to n do

 if i<>j then x[i,j]:=1000000;

 for i:=2 to n do

 x[i-1,i]:=a[i-1,i];

 for i:=1 to n do

 for j:=i-1 downto 1 do

 begin

  for k:=j to i-1 do

  x[j,i]:=min(x[j,i],x[j,k]+x[k+1,i]+a[j,i]);

 end;

 writeln(x[1,n]);

end.


你可能感兴趣的:(codevs题解)