这道题,动规,但是如果只扫一次的话,往往发现得出来的解不是最优解的,所以要扫多次,知道确保没有变化了为止,说明就有最优解了;
不一定要预处理,可以分情况讨论。
还要加油啊
1 program P1006; uses math; 2 var 3 s:array[0..1010,0..1010]of longint; 4 f:array[0..1010,0..1010]of longint; 5 i,j,k,l,m,n,ans:longint; 6 bool:boolean; 7 begin 8 read(n); 9 for i:=1 to n do 10 for j:=1 to i do 11 read(s[i,j]); 12 for i:=1 to n do 13 for j:=1 to i do 14 f[i,j]:=s[i,j]; 15 for i:=2 to n do 16 begin 17 for j:=1 to i do 18 if (j>1)and(j<i) then f[i,j]:=min(f[i-1,j],f[i-1,j-1])+s[i,j] 19 else if (j=1)or(j=i) then f[i,j]:=min(f[i-1,i-1],f[i-1,1])+s[i,j] 20 end; 21 repeat 22 bool:=true; 23 for i:=2 to n do 24 begin 25 for j:=1 to i do 26 if (j>1)and(j<i) then begin 27 if f[i,j]>f[i-1,j]+s[i,j] then begin bool:=false; f[i,j]:=f[i-1,j]+s[i,j]; end; 28 if f[i,j]>f[i-1,j-1]+s[i,j] then begin bool:=false; f[i,j]:=f[i-1,j-1]+s[i,j]; end; 29 end 30 else if (j=1)or(j=i) then begin 31 if f[i,j]>f[i-1,i-1]+s[i,j] then begin bool:=false; f[i,j]:=f[i-1,i-1]+s[i,j]; end; 32 if f[i,j]>f[i-1,1]+s[i,j] then begin bool:=false; f[i,j]:=f[i-1,1]+s[i,j]; end; 33 end 34 end; 35 for i:=2 to n do 36 begin 37 for j:=1 to i do 38 if (j>1)and(j<i) then begin 39 if f[i,j]>f[i,j-1]+s[i,j] then 40 begin 41 f[i,j]:=f[i,j-1]+s[i,j]; 42 bool:=false; 43 end; 44 if f[i,j]>f[i,j+1]+s[i,j] then 45 begin f[i,j]:=f[i,j+1]+s[i,j]; 46 bool:=false; 47 end; 48 end 49 else if(j=1) then begin 50 if f[i,j]>f[i,i]+s[i,j] then 51 begin f[i,j]:=f[i,i]+s[i,j]; 52 bool:=false; 53 end; 54 if f[i,j]>f[i,j+1]+s[i,j] then 55 begin f[i,j]:=f[i,j+1]+s[i,j]; 56 bool:=false; 57 end; 58 end 59 else if j=i then begin 60 if f[i,j]>f[i,j-1]+s[i,j] then 61 begin f[i,j]:=f[i,j-1]+s[i,j]; 62 bool:=false; 63 end; 64 if f[i,j]>f[i,1]+s[i,j] then 65 begin f[i,j]:=f[i,1]+s[i,j]; 66 bool:=false; 67 end; 68 end; 69 end; 70 until bool=true; 71 write(f[n,1]); 72 end.