結果

問題 No.2569 はじめてのおつかいHard
ユーザー loop0919loop0919
提出日時 2023-11-14 01:02:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,852 ms / 2,000 ms
コード長 1,166 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 11,904 KB
実行使用メモリ 94,976 KB
最終ジャッジ日時 2023-11-14 01:03:11
合計ジャッジ時間 14,166 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 730 ms
42,240 KB
testcase_01 AC 753 ms
42,240 KB
testcase_02 AC 770 ms
42,240 KB
testcase_03 AC 743 ms
42,240 KB
testcase_04 AC 776 ms
42,240 KB
testcase_05 AC 1,852 ms
94,080 KB
testcase_06 AC 1,745 ms
67,808 KB
testcase_07 AC 1,533 ms
88,832 KB
testcase_08 AC 1,844 ms
94,976 KB
testcase_09 AC 1,297 ms
60,672 KB
testcase_10 AC 28 ms
10,240 KB
testcase_11 AC 29 ms
10,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop
INF = 1 << 60

# ダイクストラ法
def dijkstra(start, G):
    costs = [INF]*len(G)
    costs[start] = 0
    
    pq = []
    heappush(pq, (0, start))
    
    while pq:
        now_t, now_v = heappop(pq)
        
        if costs[now_v] < now_t:
            continue
        
        for next_t, next_v in G[now_v]:
            if costs[next_v] > costs[now_v] + next_t:
                costs[next_v] = costs[now_v] + next_t
                heappush(pq, (now_t + next_t, next_v))
    
    return costs


N, M = map(int, input().split())

graph = [[] for _ in range(N)]
graph_rev = [[] for _ in range(N)]

for _ in range(M):
    u, v, t = map(int, input().split())
    u -= 1; v -= 1
    graph[u].append((t, v))
    graph_rev[v].append((t, u))

di = {
    N-2: dijkstra(N-2, graph),
    N-1: dijkstra(N-1, graph)
}

di_rev = {
    N-2: dijkstra(N-2, graph_rev),
    N-1: dijkstra(N-1, graph_rev)
}

for i in range(N-2):
    ans_1 = di_rev[N-2][i] + di[N-2][N-1] + di[N-1][i]
    ans_2 = di_rev[N-1][i] + di[N-1][N-2] + di[N-2][i]
    ans = min(ans_1, ans_2)
    
    if ans < INF:
        print(ans)
    else:
        print(-1)
0