結果

問題 No.2569 はじめてのおつかいHard
ユーザー prd_xxxprd_xxx
提出日時 2023-12-02 16:36:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 685 ms / 2,000 ms
コード長 1,403 bytes
コンパイル時間 537 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 143,068 KB
最終ジャッジ日時 2023-12-02 16:37:05
合計ジャッジ時間 6,061 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 254 ms
106,328 KB
testcase_01 AC 308 ms
106,664 KB
testcase_02 AC 249 ms
106,416 KB
testcase_03 AC 249 ms
106,336 KB
testcase_04 AC 250 ms
106,444 KB
testcase_05 AC 636 ms
142,160 KB
testcase_06 AC 685 ms
126,896 KB
testcase_07 AC 379 ms
141,104 KB
testcase_08 AC 584 ms
143,068 KB
testcase_09 AC 461 ms
117,836 KB
testcase_10 AC 37 ms
53,460 KB
testcase_11 AC 38 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N,M = map(int,input().split())
UVT = [tuple(map(int,input().split())) for _ in range(M)]
es = [[] for _ in range(N)]
rs = [[] for _ in range(N)]
for u,v,t in UVT:
    u,v = u-1,v-1
    es[v].append((u,t))
    rs[u].append((v,t))

A,B = N-1,N-2
INF = 10**18
from heapq import heappop,heappush
hq = [(0,A)]
distA = [INF] * N
distA[A] = 0
while hq:
    d,v = heappop(hq)
    if d > distA[v]: continue
    for to,c in es[v]:
        if distA[to] <= d+c: continue
        distA[to] = d+c
        heappush(hq, (d+c, to))
hq = [(0,B)]
distB = [INF] * N
distB[B] = 0
while hq:
    d,v = heappop(hq)
    if d > distB[v]: continue
    for to,c in es[v]:
        if distB[to] <= d+c: continue
        distB[to] = d+c
        heappush(hq, (d+c, to))
hq = [(0,A)]
rdistA = [INF] * N
rdistA[A] = 0
while hq:
    d,v = heappop(hq)
    if d > rdistA[v]: continue
    for to,c in rs[v]:
        if rdistA[to] <= d+c: continue
        rdistA[to] = d+c
        heappush(hq, (d+c, to))
hq = [(0,B)]
rdistB = [INF] * N
rdistB[B] = 0
while hq:
    d,v = heappop(hq)
    if d > rdistB[v]: continue
    for to,c in rs[v]:
        if rdistB[to] <= d+c: continue
        rdistB[to] = d+c
        heappush(hq, (d+c, to))

for i in range(N-2):
    ans1 = rdistA[i] + distA[B] + distB[i]
    ans2 = rdistB[i] + distB[A] + distA[i]
    ans = min(ans1, ans2)
    print(-1 if ans >= INF else ans)
0