結果
| 問題 | No.1283 Extra Fee | 
| コンテスト | |
| ユーザー |  rlangevin | 
| 提出日時 | 2023-02-05 22:56:46 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,863 ms / 2,000 ms | 
| コード長 | 1,840 bytes | 
| コンパイル時間 | 556 ms | 
| コンパイル使用メモリ | 82,176 KB | 
| 実行使用メモリ | 340,308 KB | 
| 最終ジャッジ日時 | 2024-07-04 08:23:58 | 
| 合計ジャッジ時間 | 27,878 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 30 | 
ソースコード
import sys
readline = sys.stdin.readline
from heapq import heappush, heappop
inf = float('inf')
def dijkstra(s, g, N):
    # ゴールがない場合はg=-1とする。
    def cost(v, m):
        return v * N + m
    dist = [inf] * N
    mindist = [inf] * N
    seen = [False] * N
    Q = [cost(0, s)]
    while Q:
        c, m = divmod(heappop(Q), N)
        if seen[m]:
            continue
        seen[m] = True
        dist[m] = c
        if m == g:
            return dist
        #------heapをアップデートする。--------
        for u, C in G[m]:
            if seen[u]:
                continue
            newdist = dist[m] + C
            #------------------------------------
            if newdist >= mindist[u]:
                continue
            mindist[u] = newdist
            heappush(Q, cost(newdist, u))
    return dist
def f(h, w, c):
    return h + N * w + N * N * c
N, M = map(int, readline().split())
temp = [[0] * N for i in range(N)]
for i in range(M):
    h, w, c = map(int, readline().split())
    h, w = h - 1, w - 1
    temp[h][w] = c
    
G = [[] for i in range(N * N * 2)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
for i in range(N):
    for j in range(N):
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x > N - 1 or y < 0 or y > N - 1:
                continue
            G[f(i, j, 0)].append( ( f(x, y, 0), temp[x][y] + 1))
            G[f(x, y, 0)].append( ( f(i, j, 0), temp[i][j] + 1))
            G[f(i, j, 1)].append( ( f(x, y, 1), temp[x][y] + 1))
            G[f(x, y, 1)].append( ( f(i, j, 1), temp[i][j] + 1))
            G[f(i, j, 0)].append( ( f(x, y, 1), 1))
            G[f(x, y, 0)].append( ( f(i, j, 1), 1))
            
D = dijkstra(0, -1, N * N * 2)
print(min(D[f(N - 1, N - 1, 0)], D[f(N - 1, N - 1, 1)]))           
            
            
            
        