結果

問題 No.1283 Extra Fee
ユーザー nehan_der_thalnehan_der_thal
提出日時 2020-11-06 22:46:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,478 ms / 2,000 ms
コード長 1,446 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 82,256 KB
実行使用メモリ 348,408 KB
最終ジャッジ日時 2024-04-27 23:24:36
合計ジャッジ時間 20,346 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
56,172 KB
testcase_01 AC 40 ms
54,792 KB
testcase_02 AC 39 ms
55,760 KB
testcase_03 AC 41 ms
54,712 KB
testcase_04 AC 41 ms
55,700 KB
testcase_05 AC 38 ms
54,780 KB
testcase_06 AC 45 ms
62,460 KB
testcase_07 AC 38 ms
56,252 KB
testcase_08 AC 41 ms
61,072 KB
testcase_09 AC 40 ms
56,720 KB
testcase_10 AC 39 ms
55,824 KB
testcase_11 AC 210 ms
92,264 KB
testcase_12 AC 195 ms
93,564 KB
testcase_13 AC 150 ms
87,916 KB
testcase_14 AC 319 ms
121,796 KB
testcase_15 AC 415 ms
145,108 KB
testcase_16 AC 166 ms
92,024 KB
testcase_17 AC 1,334 ms
319,144 KB
testcase_18 AC 1,259 ms
316,100 KB
testcase_19 AC 1,370 ms
328,140 KB
testcase_20 AC 1,229 ms
312,748 KB
testcase_21 AC 1,218 ms
311,288 KB
testcase_22 AC 1,121 ms
287,452 KB
testcase_23 AC 1,244 ms
323,752 KB
testcase_24 AC 1,245 ms
328,328 KB
testcase_25 AC 1,387 ms
334,376 KB
testcase_26 AC 1,392 ms
333,916 KB
testcase_27 AC 1,366 ms
333,868 KB
testcase_28 AC 1,432 ms
334,432 KB
testcase_29 AC 1,478 ms
348,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys;input=sys.stdin.readline
from heapq import heappush, heappop
INF = 10**18
def dijkstra(N, G, s):
    dist = [INF] * N
    que = [(0, s)]
    dist[s] = 0
    while que:
        c, v = heappop(que)
        if dist[v] < c:
            continue
        for t, cost in G[v]:
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heappush(que, (dist[t], t))
    return dist
N, M = map(int, input().split())
from collections import defaultdict
D = defaultdict(int)
U = N**2
for _ in range(M):
    a, b, c = map(int, input().split())
    a-=1
    b-=1
    D[a*N+b] = c
    D[a*N+b+U] = c

G = [set() for _ in range(2*(N**2))]
for i in range(N):
    for j in range(N):
        c = i*N+j
        if j!=N-1:
            G[c].add((c+1, 1+D[c+1]))
        if j!=0:
            G[c].add((c-1, 1+D[c-1]))
        if i!=0:
            G[c].add((c-N, 1+D[c-N]))
        if i!=N-1:
            G[c].add((c+N, 1+D[c+N]))

        if j!=N-1:
            G[c].add((c+1+U, 1))
        if j!=0:
            G[c].add((c-1+U, 1))
        if i!=0:
            G[c].add((c-N+U, 1))
        if i!=N-1:
            G[c].add((c+N+U, 1))

        c += U
        if j!=N-1:
            G[c].add((c+1, 1+D[c+1]))
        if j!=0:
            G[c].add((c-1, 1+D[c-1]))
        if i!=0:
            G[c].add((c-N, 1+D[c-N]))
        if i!=N-1:
            G[c].add((c+N, 1+D[c+N]))

X = dijkstra(2*(N**2), G, 0)
print(X[-1])

0