結果

問題 No.807 umg tours
ユーザー rlangevinrlangevin
提出日時 2023-02-11 20:14:28
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,182 ms / 4,000 ms
コード長 1,268 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 162,572 KB
最終ジャッジ日時 2023-09-22 15:38:49
合計ジャッジ時間 16,735 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
75,788 KB
testcase_01 AC 83 ms
75,816 KB
testcase_02 AC 85 ms
75,772 KB
testcase_03 AC 84 ms
75,636 KB
testcase_04 AC 81 ms
75,844 KB
testcase_05 AC 83 ms
75,808 KB
testcase_06 AC 84 ms
75,928 KB
testcase_07 AC 88 ms
76,388 KB
testcase_08 AC 76 ms
71,328 KB
testcase_09 AC 74 ms
71,300 KB
testcase_10 AC 75 ms
71,380 KB
testcase_11 AC 729 ms
135,796 KB
testcase_12 AC 960 ms
124,716 KB
testcase_13 AC 880 ms
143,136 KB
testcase_14 AC 445 ms
106,176 KB
testcase_15 AC 369 ms
99,176 KB
testcase_16 AC 958 ms
147,196 KB
testcase_17 AC 1,146 ms
158,344 KB
testcase_18 AC 1,182 ms
157,756 KB
testcase_19 AC 1,131 ms
156,772 KB
testcase_20 AC 644 ms
127,384 KB
testcase_21 AC 695 ms
132,136 KB
testcase_22 AC 362 ms
100,732 KB
testcase_23 AC 328 ms
96,732 KB
testcase_24 AC 493 ms
147,124 KB
testcase_25 AC 1,142 ms
162,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline
from heapq import heappush, heappop
inf = float('inf')


def dijkstra(s, g, N):
    # ゴールがない場合はg=-1とする。

    def cost(v, m):
        return v * N + m

    dist = [inf] * N
    mindist = [inf] * N
    seen = [False] * N
    Q = [cost(0, s)]
    while Q:
        c, m = divmod(heappop(Q), N)
        if seen[m]:
            continue
        seen[m] = True
        dist[m] = c
        if m == g:
            return dist

        #------heapをアップデートする。--------
        for u, C in G[m]:
            if seen[u]:
                continue
            newdist = dist[m] + C

            #------------------------------------
            if newdist >= mindist[u]:
                continue
            mindist[u] = newdist
            heappush(Q, cost(newdist, u))
    return dist

N, M = map(int, readline().split())
G = [[] for i in range(2 * N)]
for i in range(M):
    a, b, c, = map(int, readline().split())
    a, b = a - 1, b - 1
    G[a].append((b, c))
    G[b].append((a, c))
    G[a + N].append((b + N, c))
    G[b + N].append((a + N, c))
    G[a].append((b + N, 0))
    G[b].append((a + N, 0))
    
D = dijkstra(0, -1, 2 * N)
for i in range(N):
    print(D[i] + min(D[i], D[i + N]))
0