結果

問題 No.2095 High Rise
ユーザー roaris
提出日時 2022-10-07 21:58:36
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 937 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 82,184 KB
実行使用メモリ 129,268 KB
最終ジャッジ日時 2024-06-12 07:17:17
合計ジャッジ時間 14,451 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from heapq import *

N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
dist = [[[10**18]*M for _ in range(N)] for _ in range(2)]
pq = []

for i in range(M):
    dist[0][0][i] = 0
    heappush(pq, (0, 100*i))

while pq:
    d, xyf = heappop(pq)
    xy, f = divmod(xyf, 100)
    x, y = divmod(xy, 10**5)
    
    if dist[f][x][y]<d:
        continue
    
    for nx, ny in [(x, y-1), (x, y+1), (x+1, y)]:
        if 0<=nx<N and 0<=ny<M:
            if (nx, ny)==(x+1, y):
                w = A[nx][ny] if f==1 else A[x][y]+A[nx][ny]
                nf = 1
            else:
                w = 0
                nf = 0
            
            if dist[nf][nx][ny]>dist[f][x][y]+w:
                dist[nf][nx][ny] = dist[f][x][y]+w
                heappush(pq, (dist[nf][nx][ny], (nx*10**5+ny)*100+nf))

ans = min(min(dist[0][N-1]), min(dist[1][N-1]))
print(ans)
0