vijos p1006(晴天小猪历险记之hill)(此题需注意一下)

这道题,动规,但是如果只扫一次的话,往往发现得出来的解不是最优解的,所以要扫多次,知道确保没有变化了为止,说明就有最优解了;

不一定要预处理,可以分情况讨论。

还要加油啊

 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.   

转载于:https://www.cnblogs.com/zyxx233/archive/2012/10/16/2726589.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值