結果

問題 No.1283 Extra Fee
ユーザー rlangevinrlangevin
提出日時 2023-10-11 11:52:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,997 ms / 2,000 ms
コード長 1,916 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 87,404 KB
実行使用メモリ 341,280 KB
最終ジャッジ日時 2023-10-11 11:52:56
合計ジャッジ時間 30,815 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,632 KB
testcase_01 AC 79 ms
71,304 KB
testcase_02 AC 80 ms
71,324 KB
testcase_03 AC 78 ms
71,332 KB
testcase_04 AC 79 ms
71,432 KB
testcase_05 AC 79 ms
71,456 KB
testcase_06 AC 90 ms
75,708 KB
testcase_07 AC 79 ms
71,168 KB
testcase_08 AC 88 ms
75,948 KB
testcase_09 AC 90 ms
76,156 KB
testcase_10 AC 84 ms
75,208 KB
testcase_11 AC 232 ms
92,104 KB
testcase_12 AC 245 ms
94,508 KB
testcase_13 AC 206 ms
88,608 KB
testcase_14 AC 394 ms
122,944 KB
testcase_15 AC 593 ms
148,928 KB
testcase_16 AC 374 ms
93,740 KB
testcase_17 AC 1,707 ms
308,484 KB
testcase_18 AC 1,996 ms
322,388 KB
testcase_19 AC 1,971 ms
332,792 KB
testcase_20 AC 1,917 ms
317,088 KB
testcase_21 AC 1,863 ms
324,044 KB
testcase_22 AC 1,696 ms
304,944 KB
testcase_23 AC 1,645 ms
329,492 KB
testcase_24 AC 1,781 ms
341,092 KB
testcase_25 AC 1,997 ms
340,900 KB
testcase_26 AC 1,971 ms
340,672 KB
testcase_27 AC 1,907 ms
340,708 KB
testcase_28 AC 1,909 ms
341,280 KB
testcase_29 AC 1,800 ms
331,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline
from heapq import heappush, heappop
inf = float('inf')


def dijkstra(s, g, N):
    # ゴールがない場合はg=-1とする。
    g0 = f(N-1, N-1, 0)
    g1 = f(N-1, N-1, 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 == g0 or m == g1:
            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, f(N - 1, N - 1, 1), N * N * 2)
print(min(D[f(N - 1, N - 1, 0)], D[f(N - 1, N - 1, 1)]))           
0