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