题意:
思路:
写了两种版本
考场版本
1 var dp:array[1..300,1..300]of longint; 2 t:array[1..300,1..300]of longint; 3 n,m,i,j,k,oo,ans,d1:longint; 4 5 6 function min(x,y:int64):int64; 7 begin 8 if x<y then exit(x); 9 exit(y); 10 end; 11 12 begin 13 assign(input,'room.in'); reset(input); 14 assign(output,'room.out'); rewrite(output); 15 readln(n,m,d1); 16 oo:=maxlongint>>1; 17 for i:=1 to n do 18 for j:=1 to m do read(t[i,j]); 19 20 for i:=2 to n do 21 begin 22 for j:=1 to m do 23 for k:=1 to m do 24 if j<>k then dp[j,k]:=dp[j,k]+t[i-1,j]+t[i-1,k] 25 else dp[j,k]:=oo; 26 27 for j:=2 to m do 28 for k:=1 to m do dp[j,k]:=min(dp[j,k],dp[j-1,k]+d1); 29 30 for j:=m-1 downto 1 do 31 for k:=1 to m do dp[j,k]:=min(dp[j,k],dp[j+1,k]+d1); 32 33 for j:=1 to m do 34 for k:=2 to m do dp[j,k]:=min(dp[j,k],dp[j,k-1]+d1); 35 36 for j:=1 to m do 37 for k:=m-1 downto 1 do dp[j,k]:=min(dp[j,k],dp[j,k+1]+d1); 38 end; 39 ans:=oo; 40 for i:=1 to m do 41 for j:=1 to m do 42 if i<>j then ans:=min(ans,dp[i,j]+t[n,i]+t[n,j]); 43 writeln(ans); 44 {for i:=1 to n do 45 for j:=1 to m do 46 for k:=1 to m do writeln(i,' ',j,' ',k,' ',dp[i,j,k]+t[i,j]+t[i,k]);} 47 close(input); 48 close(output); 49 end.
赛后自测版本
1 var dp:array[0..300,1..300,1..300]of longint; 2 t:array[1..300,1..300]of longint; 3 n,m,d1,i,j,k,ans:longint; 4 5 function min(x,y:longint):longint; 6 begin 7 if x<y then exit(x); 8 exit(y); 9 end; 10 11 begin 12 assign(input,'room.in'); reset(input); 13 assign(output,'room.out'); rewrite(output); 14 readln(n,m,d1); 15 16 for i:=1 to n do 17 for j:=1 to m do read(t[i,j]); 18 19 for i:=1 to n do 20 begin 21 for j:=1 to m do 22 for k:=1 to m do 23 if j<>k then dp[i,j,k]:=dp[i-1,j,k]+t[i,j]+t[i,k] 24 else dp[i,j,k]:=maxlongint>>1; 25 26 for j:=2 to m do 27 for k:=1 to m do 28 if (j<>k)and(j-1<>k) then dp[i,j,k]:=min(dp[i,j,k],dp[i,j-1,k]+d1); 29 30 for j:=m-1 downto 1 do 31 for k:=1 to m do 32 if (j<>k)and(j+1<>k) then dp[i,j,k]:=min(dp[i,j,k],dp[i,j+1,k]+d1); 33 34 for j:=1 to m do 35 for k:=2 to m do 36 if (j<>k)and(k-1<>j) then dp[i,j,k]:=min(dp[i,j,k],dp[i,j,k-1]+d1); 37 38 39 40 for j:=1 to m do 41 for k:=m-1 downto 1 do 42 if (j<>k)and(k+1<>j) then dp[i,j,k]:=min(dp[i,j,k],dp[i,j,k+1]+d1); 43 end; 44 45 ans:=maxlongint; 46 for i:=1 to m do 47 for j:=1 to m do 48 if i<>j then ans:=min(ans,dp[n,i,j]); 49 writeln(ans); 50 51 close(input); 52 close(output); 53 end.