結果

問題 No.2569 はじめてのおつかいHard
ユーザー lloyzlloyz
提出日時 2024-01-12 04:11:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 811 ms / 2,000 ms
コード長 1,010 bytes
コンパイル時間 424 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 139,160 KB
最終ジャッジ日時 2024-01-12 04:11:27
合計ジャッジ時間 8,127 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 295 ms
91,528 KB
testcase_01 AC 268 ms
91,528 KB
testcase_02 AC 273 ms
91,272 KB
testcase_03 AC 266 ms
91,400 KB
testcase_04 AC 299 ms
91,528 KB
testcase_05 AC 768 ms
139,160 KB
testcase_06 AC 811 ms
108,524 KB
testcase_07 AC 493 ms
132,764 KB
testcase_08 AC 733 ms
138,408 KB
testcase_09 AC 548 ms
101,000 KB
testcase_10 AC 42 ms
55,604 KB
testcase_11 AC 82 ms
55,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from heapq import heappop, heappush

n, m = map(int, input().split())
G, RG = defaultdict(list), defaultdict(list)
for _ in range(m):
    u, v, t = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append((v, t))
    RG[v].append((u, t))

INF = 10**18
def dijkstra(start, G):
    C = [INF for _ in range(n)]
    C[start] = 0
    H = [(0, start)]
    while H:
        ct, cp = heappop(H)
        if ct > C[cp]:
            continue
        for np, dt in G[cp]:
            nt = ct + dt
            if nt >= C[np]:
                continue
            C[np] = nt
            heappush(H, (nt, np))
    return C

CG = {n - 2: dijkstra(n - 2, G), n - 1: dijkstra(n - 1, G)}
CRG = {n - 2: dijkstra(n - 2, RG), n - 1: dijkstra(n - 1, RG)}
for i in range(n - 2):
    ans = INF
    ans = min(
        ans,
        CRG[n - 2][i] + CG[n - 2][n - 1] + CG[n - 1][i],
        CRG[n - 1][i] + CG[n - 1][n - 2] + CG[n - 2][i]
    )
    if ans == INF:
        ans = -1
    print(ans)
0