結果

問題 No.1283 Extra Fee
ユーザー neterukunneterukun
提出日時 2020-11-06 22:05:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,674 ms / 2,000 ms
コード長 1,501 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 117,792 KB
最終ジャッジ日時 2023-08-10 05:59:48
合計ジャッジ時間 19,783 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,380 KB
testcase_01 AC 71 ms
71,136 KB
testcase_02 AC 71 ms
71,408 KB
testcase_03 AC 70 ms
71,352 KB
testcase_04 AC 70 ms
71,552 KB
testcase_05 AC 70 ms
71,608 KB
testcase_06 AC 74 ms
71,552 KB
testcase_07 AC 72 ms
71,288 KB
testcase_08 AC 72 ms
71,188 KB
testcase_09 AC 71 ms
71,340 KB
testcase_10 AC 74 ms
71,296 KB
testcase_11 AC 240 ms
80,708 KB
testcase_12 AC 237 ms
80,948 KB
testcase_13 AC 190 ms
80,304 KB
testcase_14 AC 313 ms
85,132 KB
testcase_15 AC 440 ms
89,672 KB
testcase_16 AC 215 ms
81,040 KB
testcase_17 AC 1,569 ms
107,920 KB
testcase_18 AC 1,165 ms
114,228 KB
testcase_19 AC 1,165 ms
116,004 KB
testcase_20 AC 1,125 ms
114,944 KB
testcase_21 AC 1,106 ms
114,956 KB
testcase_22 AC 969 ms
110,836 KB
testcase_23 AC 1,001 ms
113,372 KB
testcase_24 AC 1,005 ms
112,804 KB
testcase_25 AC 1,204 ms
117,788 KB
testcase_26 AC 1,158 ms
117,792 KB
testcase_27 AC 1,149 ms
117,684 KB
testcase_28 AC 1,140 ms
117,376 KB
testcase_29 AC 1,674 ms
108,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


dd = ((1, 0), (-1, 0), (0, 1), (0, -1))
def dijkstra() -> list:
    INF = 10 ** 18
    dist = [[[INF] * n for i in range(n)] for i in range(2)]
    dist[0][0][0] = 0
    q = [(0, 0, 0, 0)] # q = [(startからの距離, i, j, cnt)]
    while q:
        d, i, j, cnt = heapq.heappop(q)
        if dist[cnt][i][j] < d:
            continue
        for di, dj in dd:
            nxt_i, nxt_j = i + di, j + dj
            if not(0 <= nxt_i < h and 0 <= nxt_j < w):
                continue
            if cnt == 1:
                if dist[1][i][j] + 1 + cost[nxt_i][nxt_j] < dist[1][nxt_i][nxt_j]:
                    dist[1][nxt_i][nxt_j] = dist[1][i][j] + 1 + cost[nxt_i][nxt_j]
                    heapq.heappush(q, (dist[1][nxt_i][nxt_j], nxt_i, nxt_j, 1))
            else:
                if dist[0][i][j] + 1 + cost[nxt_i][nxt_j] < dist[0][nxt_i][nxt_j]:
                    dist[0][nxt_i][nxt_j] = dist[0][i][j] + 1 + cost[nxt_i][nxt_j]
                    heapq.heappush(q, (dist[0][nxt_i][nxt_j], nxt_i, nxt_j, 0))
                if dist[0][i][j] + 1 < dist[1][nxt_i][nxt_j]:
                    dist[1][nxt_i][nxt_j] = dist[0][i][j] + 1
                    heapq.heappush(q, (dist[1][nxt_i][nxt_j], nxt_i, nxt_j, 1))
    return dist


n, m = map(int, input().split())
h, w = n, n
info = [list(map(int, input().split())) for i in range(m)]


cost = [[0] * n for i in range(n)]
for i, j, c in info:
    i -= 1
    j -= 1
    cost[i][j] = c

dist = dijkstra()
print(dist[1][-1][-1])
0