結果

問題 No.1283 Extra Fee
ユーザー nehan_der_thalnehan_der_thal
提出日時 2020-11-06 22:46:39
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,645 ms / 2,000 ms
コード長 1,446 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 351,704 KB
最終ジャッジ日時 2023-08-10 06:06:43
合計ジャッジ時間 24,490 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,892 KB
testcase_01 AC 89 ms
71,704 KB
testcase_02 AC 91 ms
71,740 KB
testcase_03 AC 93 ms
71,632 KB
testcase_04 AC 88 ms
71,608 KB
testcase_05 AC 89 ms
71,888 KB
testcase_06 AC 98 ms
76,716 KB
testcase_07 AC 89 ms
71,420 KB
testcase_08 AC 96 ms
76,580 KB
testcase_09 AC 91 ms
71,476 KB
testcase_10 AC 94 ms
71,824 KB
testcase_11 AC 274 ms
93,144 KB
testcase_12 AC 261 ms
95,628 KB
testcase_13 AC 197 ms
88,340 KB
testcase_14 AC 394 ms
122,900 KB
testcase_15 AC 519 ms
146,756 KB
testcase_16 AC 238 ms
94,536 KB
testcase_17 AC 1,559 ms
323,700 KB
testcase_18 AC 1,446 ms
320,468 KB
testcase_19 AC 1,549 ms
325,644 KB
testcase_20 AC 1,399 ms
311,308 KB
testcase_21 AC 1,429 ms
319,440 KB
testcase_22 AC 1,273 ms
290,084 KB
testcase_23 AC 1,424 ms
320,292 KB
testcase_24 AC 1,419 ms
331,384 KB
testcase_25 AC 1,595 ms
339,044 KB
testcase_26 AC 1,580 ms
339,372 KB
testcase_27 AC 1,542 ms
339,068 KB
testcase_28 AC 1,603 ms
339,156 KB
testcase_29 AC 1,645 ms
351,704 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