Mobile Service
作者:互联网
原文链接:http://www.cnblogs.com/lexluthor/archive/2011/10/17/2215817.html
这是一道很DT的DP,你认为呢?
const infinite=1000000000; var w:array[0..200,0..200]of longint; n,i,j,x1,x2,ans,l:longint; p:array[0..200]of longint; f:array[0..3,0..200,0..200]of longint; function min(a,b:longint):longint; begin if a=-1 then exit(b) else if a<b then exit(a) else exit(b); end; begin fillchar(ans,sizeof(ans),$3f); filldword(f,sizeof(f)div 4,infinite); readln(l,n); for i:=1 to l do for j:=1 to l do read(w[i,j]); for i:=1 to n do read(p[i]); f[1,1,2]:=0; p[0]:=3; for i:=1 to n do begin for x1:=1 to l do for x2:=1 to l do begin f[i mod 2+1,p[i-1],x2]:=infinite; f[i mod 2+1,x1,p[i-1]]:=infinite; f[i mod 2 +1,x1,x2]:=infinite; end; for x1:=1 to l do for x2:=1 to l do if f[(i-1)mod 2+1,x1,x2]<>infinite then begin f[i mod 2+1,p[i-1],x2]:=min(f[i mod 2+1,p[i-1],x2],f[(i-1)mod 2+1,x1,x2]+w[x1,p[i]]); f[i mod 2+1,x1,p[i-1]]:=min(f[i mod 2+1,x1,p[i-1]],f[(i-1)mod 2+1,x1,x2]+w[x2,p[i]]); f[i mod 2 +1,x1,x2]:=min(f[i mod 2 +1,x1,x2],f[(i-1)mod 2+1,x1,x2]+w[p[i-1],p[i]]); end; end; for x1:=1 to l do for x2:=1 to l do begin if(f[n mod 2+1,x1,x2]<>-1)and(f[n mod 2+1,x1,x2]<ans) then ans:=f[n mod 2+1,x1,x2]; end; writeln(ans); end.
对于这个滚存数组,是否有更好的写法呢?
转载于:https://www.cnblogs.com/lexluthor/archive/2011/10/17/2215817.html
标签:200,Service,..,Mobile,longint,x2,x1,mod 来源: https://blog.csdn.net/weixin_30919429/article/details/98330148