結果

問題 No.2569 はじめてのおつかいHard
ユーザー navel_tosnavel_tos
提出日時 2023-12-02 16:22:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,980 ms / 2,000 ms
コード長 1,300 bytes
コンパイル時間 142 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 155,228 KB
最終ジャッジ日時 2023-12-02 16:23:08
合計ジャッジ時間 14,980 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,237 ms
132,796 KB
testcase_01 AC 1,278 ms
131,912 KB
testcase_02 AC 1,281 ms
132,508 KB
testcase_03 AC 1,260 ms
132,404 KB
testcase_04 AC 1,256 ms
132,468 KB
testcase_05 AC 943 ms
133,768 KB
testcase_06 AC 1,980 ms
155,228 KB
testcase_07 AC 469 ms
128,740 KB
testcase_08 AC 884 ms
133,640 KB
testcase_09 AC 1,790 ms
152,052 KB
testcase_10 AC 33 ms
53,460 KB
testcase_11 AC 33 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#緑以下Ex-A

import heapq as hq
import sys; input = sys.stdin.readline

#入力受取
N,M = map(int,input().split())

#G[i][x]: xは0なら行き、1なら帰り
G = [[[] for _ in range(2)] for _ in range(N)]
for _ in range(M):
    u,v,t = map(lambda x:int(x)-1,input().split()); t+=1
    G[u][0].append((v,t)); G[v][1].append((u,t))

#dist_1[k][x]: x=0なら町N-2→町k, x=1なら町k→町N-2
#dist_1[2*k+x] に一次元化
dist_1 = [10**18]*2*N
Q = [(0,N-2,0),(0,N-2,1)]
while Q:
    time,now,x = hq.heappop(Q)
    if dist_1[2*now+x] < time: continue
    dist_1[2*now+x] = time
    for next,cost in G[now][x]:
        if dist_1[2*next+x] > time + cost:
            hq.heappush(Q,(time+cost, next, x))

#dist_2[k]: 町N-1→町k, 町k→町N-1
#dist_2[2*k+x]に一次元化
dist_2 = [10**18]*2*N
Q = [(0,N-1,0),(0,N-1,1)]
while Q:
    time,now,x = hq.heappop(Q)
    if dist_2[2*now+x] < time: continue
    dist_2[2*now+x] = time
    for next,cost in G[now][x]:
        if dist_2[2*next+x] > time + cost:
            hq.heappush(Q,(time+cost, next, x))

#順に答えを出力
for k in range(N-2):
    ans = 10**18
    ans = min(ans, dist_1[2*k+1] + dist_1[2*(N-1)+0] + dist_2[2*k+0])
    ans = min(ans, dist_2[2*k+1] + dist_2[2*(N-2)+0] + dist_1[2*k+0])
    print(ans if ans < 10**18 else -1)
0