結果
| 問題 | 
                            No.2328 Build Walls
                             | 
                    
| コンテスト | |
| ユーザー | 
                             navel_tos
                         | 
                    
| 提出日時 | 2023-05-29 19:27:21 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 915 bytes | 
| コンパイル時間 | 545 ms | 
| コンパイル使用メモリ | 82,176 KB | 
| 実行使用メモリ | 126,764 KB | 
| 最終ジャッジ日時 | 2024-12-28 10:13:20 | 
| 合計ジャッジ時間 | 29,434 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 33 TLE * 1 | 
ソースコード
#MMA Contest 015 G
'''
DPみを感じる。こちらのほうが解きやすい・・・のか?
単に状態をもったDPをしたらTLEするに決まっている。
大胆予想: 左から右に王将の要領で移動する。右端にたどり着く最小コストを求めよ。
'''
import heapq as hq
f=lambda:list(map(int,input().split()))
H,W=f(); A=[[-1]*W]+[f() for _ in range(H-2)]+[[-1]*W]
Q=[]; cost=[[2*10**18]*W for _ in range(H)]
for h in range(1,H-1): hq.heappush(Q,(A[h][0],h,0)) if A[h][0]>=0 else None
while Q:
    dist,h,w=hq.heappop(Q)
    if dist>=cost[h][w]: continue
    cost[h][w]=dist
    for x,y in [(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1)]:
        if 0<=h+x<H and 0<=w+y<W and A[h+x][w+y]>=0:
            if cost[h+x][w+y]>dist+A[h+x][w+y]: hq.heappush(Q,(dist+A[h+x][w+y],h+x,w+y))
ans=min(cost[h][-1] for h in range(H))
print(ans) if ans<2*10**18 else print(-1)
            
            
            
        
            
navel_tos