結果

問題 No.1283 Extra Fee
ユーザー neterukunneterukun
提出日時 2020-11-06 22:05:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,481 ms / 2,000 ms
コード長 1,501 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 116,148 KB
最終ジャッジ日時 2024-04-27 23:18:07
合計ジャッジ時間 16,524 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,704 KB
testcase_01 AC 37 ms
53,000 KB
testcase_02 AC 36 ms
53,812 KB
testcase_03 AC 38 ms
53,504 KB
testcase_04 AC 40 ms
53,580 KB
testcase_05 AC 33 ms
53,144 KB
testcase_06 AC 36 ms
54,320 KB
testcase_07 AC 32 ms
53,624 KB
testcase_08 AC 35 ms
55,060 KB
testcase_09 AC 39 ms
54,488 KB
testcase_10 AC 37 ms
54,320 KB
testcase_11 AC 190 ms
79,316 KB
testcase_12 AC 184 ms
79,080 KB
testcase_13 AC 144 ms
78,476 KB
testcase_14 AC 258 ms
83,388 KB
testcase_15 AC 360 ms
87,632 KB
testcase_16 AC 169 ms
79,800 KB
testcase_17 AC 1,371 ms
106,740 KB
testcase_18 AC 1,012 ms
113,296 KB
testcase_19 AC 983 ms
114,464 KB
testcase_20 AC 974 ms
112,448 KB
testcase_21 AC 947 ms
112,728 KB
testcase_22 AC 840 ms
108,596 KB
testcase_23 AC 847 ms
111,964 KB
testcase_24 AC 868 ms
111,340 KB
testcase_25 AC 1,097 ms
115,892 KB
testcase_26 AC 1,004 ms
116,148 KB
testcase_27 AC 985 ms
115,556 KB
testcase_28 AC 1,010 ms
115,588 KB
testcase_29 AC 1,481 ms
107,636 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