比赛 20101110 评测结果 AAAAAAAAAAAAATTTTTAT
题目名称 移动服务 最终得分 70
用户昵称 belong.zmx 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-10 21:38:04
显示代码纯文本
program service(input,output);
var
 c:array[1..200,1..200]of longint;
 n,m:longint;
 p:array[0..1000]of longint;
 f:array[0..1,1..200,1..200]of longint;
 i,j,k,ans:longint;

function min(a,b:longint):longint;
begin
 if a>b then min:=b else min:=a;
end;

begin
 assign(input,'service.in');
 reset(input);
 readln(m,n);
 for i:=1 to m do
 begin
  for j:=1 to m do read(c[i,j]);
  readln;
 end;
 for i:=1 to n do read(p[i]);
 close(input);

 for i:=0 to 1 do
  for j:=1 to m do
   for k:=1 to m do f[i,j,k]:=maxlongint;

 f[0,1,2]:=0;
 f[0,2,1]:=0;
 p[0]:=3;
 for i:=0 to n-1 do
 begin
  for j:=1 to m do
   for k:=1 to m do f[(i+1)mod 2,j,k]:=maxlongint;
  for j:=1 to m do
   for k:=1 to m do
   begin
    if (f[i mod 2,j,k]<maxlongint)and(j<>k) then
    begin
     if (k<>p[i+1])and(p[i]<>p[i+1]) then
     begin
      f[(i+1) mod 2,k,p[i]]:=min(f[(i+1) mod 2,k,p[i+1]],f[i mod 2,j,k]+c[j,p[i+1]]);
      f[(i+1) mod 2,p[i],k]:=min(f[(i+1) mod 2,p[i],k],f[(i+1) mod 2,k,p[i]]);
     end;
     if (j<>p[i+1])and(p[i]<>p[i+1]) then
     begin
      f[(i+1) mod 2,j,p[i]]:=min(f[(i+1) mod 2,j,p[i+1]],f[i mod 2,j,k]+c[k,p[i+1]]);
      f[(i+1) mod 2,p[i],j]:=min(f[(i+1) mod 2,p[i],j],f[(i+1) mod 2,j,p[i]]);
     end;
     if (j<>p[i+1])and(k<>p[i+1]) then
     begin
      f[(i+1) mod 2,j,k]:=min(f[(i+1) mod 2,j,k],f[i mod 2,j,k]+c[p[i],p[i+1]]);
      f[(i+1) mod 2,k,j]:=min(f[(i+1) mod 2,k,j],f[(i+1) mod 2,j,k]);
     end;
    end;
   end;
 end;

 assign(output,'service.out');
 rewrite(output);
 ans:=maxlongint;
 for i:=1 to m do
  for j:=1 to m do
   ans:=min(ans,f[n mod 2,i,j]);
 writeln(ans);
 close(output);
end.