var

i, j, n, x, y : longint;

a : array[1 .. 1000, 1 .. 1000] of longint;

v : array[1 .. 1000, 1 .. 1000] of longint;

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

begin

if a = 0 then exit(b);

if a < b then exit(a) else exit(b);

end;

begin

fillchar(a, sizeof(a), 0);

readln(n);

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

v[n][1] := a[n][1];

for i := n downto 1 do

begin

if i < n then

begin

x := i + 1;

for j := 1 to i do

begin

y := j + 1;

v[i][j] := min(v[i][j], v[x][y] + a[i][j]);

y := j;

v[i][j] := min(v[i][j], v[x][y] + a[i][j]);

if j = 1 then

begin

y := i + 1;

v[i][j] := min(v[i][j], v[x][y] + a[i][j]);

end;

if j = i then

begin

y := 1;

v[i][j] := min(v[i][j], v[x][y] + a[i][j]);

end;

end; end;

x := i;

for j := 1 to i do

begin

y := j - 1;

if y = 0 then y := i;

v[i][j] := min(v[i][j], v[x][y] + a[i][j]);

end;

for j := i downto 1 do

begin

y := j + 1;

if y > i then y := 1;

v[i][j] := min(v[i][j], v[x][y] + a[i][j]);

end;

end;

writeln(v[1][1]);

end.

0 条评论

目前还没有评论...

信息

ID
1006
难度
7
分类
动态规划 点击显示
标签
递交数
9118
已通过
2089
通过率
23%
被复制
29
上传者