比赛 NOIP2008集训模拟1 评测结果 ATAWAATTTT
题目名称 埃雷萨拉斯的宝藏 最终得分 40
用户昵称 lc 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-11-10 11:23:45
显示代码纯文本
program e4;
 const
  d:array[1..4,1..2] of integer=((1,0),(-1,0),(0,1),(0,-1));
 var
     n,p,h:longint;
     g:array[1..50,1..50] of longint;
     va:array[1..2500] of longint;
     a:array[1..50,1..50] of longint;

procedure init;
 var
     i,j:longint;
 begin
  readln(n,p,h);
  for i:=1 to p do
  read(Va[i]);
  for i:=1 to n do
   for j:=1 to n do
   read(a[i,j]);
 end;


procedure fill(x,y:longint);
  var
     i,j,xx,yy,tm:longint;

  begin
   for i:=1 to n do
    for j:=1 to n do
    if a[i,j]=a[x,y]
    then begin
         tm:=g[x,y];
         if (tm<g[i,j]) or (g[i,j]=-1)
         then begin
              g[i,j]:=tm;
              fill(i,j);
              end;
         end;

   for i:=1 to 4 do
    begin
     xx:=x+d[i,1]; yy:=y+d[i,2];
     if (xx>=1) and (xx<=n) and (yy>=1) and (yy<=n)
     then if a[x,y]<>a[xx,yy]
          then begin
               tm:=g[x,y]+va[a[xx,yy]];
               if (tm<g[xx,yy]) or (g[xx,yy]=-1)
               then begin
                    g[xx,yy]:=tm;
                    fill(xx,yy);
                    end;
               end;
    end;
 end;


procedure main;
 var
    i,ans:longint;

 begin
  fillchar(g,sizeof(g),$FF);
  for i:=1 to n do
    begin
    if (va[a[1,i]]<g[1,i]) or (g[1,i]=-1)
    then begin
         g[1,i]:=va[a[1,i]];
         fill(1,i);
         end;
    end;
  ans:=maxlongint;
  for i:=1 to n do if g[n,i]<ans then ans:=g[n,i];
  writeln(h-ans);
 end;

begin
 assign(input,'eldrethalas.in');
 assign(output,'eldrethalas.out');
 reset(input); rewrite(output);
 init;
 main;
 close(input); close(output);
end.