水dp,状态表示三个司机当前在哪所用最小耗油,因为有一个一定在当前点所以可以压掉一维
1 var f,w,d:array[0..210,0..210] of longint; 2 a:array[0..1010] of longint; 3 x,y,i,j,k,n,ans:longint; 4 5 function min(a,b:longint):longint; 6 begin 7 if a>b then exit(b) else exit(a); 8 end; 9 10 procedure clear; 11 var i,j:longint; 12 begin 13 for i:=1 to n do 14 for j:=i to n do 15 f[i,j]:=1000000007; 16 end; 17 18 procedure get(a,b:longint); 19 begin 20 if a>b then 21 begin 22 x:=b; 23 y:=a; 24 end 25 else begin 26 x:=a; 27 y:=b; 28 end; 29 end; 30 31 begin 32 readln(n); 33 for i:=1 to n do 34 begin 35 for j:=1 to n do 36 begin 37 read(d[i,j]); 38 w[i,j]:=1000000007; 39 end; 40 readln; 41 end; 42 w[2,3]:=0; 43 a[0]:=1; 44 i:=0; 45 while not eoln do 46 begin 47 inc(i); 48 read(a[i]); 49 clear; 50 for j:=1 to n do 51 for k:=j to n do 52 begin 53 f[j,k]:=min(f[j,k],w[j,k]+d[a[i-1],a[i]]); 54 get(a[i-1],j); 55 f[x,y]:=min(f[x,y],w[j,k]+d[k,a[i]]); 56 get(a[i-1],k); 57 f[x,y]:=min(f[x,y],w[j,k]+d[j,a[i]]); 58 end; 59 w:=f; 60 end; 61 ans:=2147483647; 62 for i:=1 to n do 63 begin 64 for j:=i to n do 65 ans:=min(ans,f[i,j]); 66 end; 67 writeln(ans); 68 end.