結果

問題 No.807 umg tours
ユーザー ntudantuda
提出日時 2021-12-01 22:59:42
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,516 bytes
コンパイル時間 358 ms
コンパイル使用メモリ 86,628 KB
実行使用メモリ 230,724 KB
最終ジャッジ日時 2023-09-18 08:59:36
合計ジャッジ時間 26,898 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 98 ms
76,184 KB
testcase_07 WA -
testcase_08 AC 77 ms
71,312 KB
testcase_09 AC 76 ms
71,288 KB
testcase_10 AC 78 ms
71,100 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 TLE -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in range(M)]
E = [[] for _ in range(N)]
for a,b,c in ABC:
    E[a-1].append([b-1,c])
    E[b-1].append([a-1,c])
import heapq
D = [float('inf')] * N
D2 = [[float('inf'), 0] for _ in range(N)]

#普通のダイクストラ
def dijkstra():
    D[0] = 0
    q = []
    heapq.heappush(q, [0, 0])
    while len(q) > 0:
        # ヒープから取り出し
        _, u = heapq.heappop(q)
        for i in E[u]:
            if D[i[0]] > D[u] + i[1]:
                # 頂点までのコストが更新できれば更新してヒープに登録
                D[i[0]] = D[u] + i[1]
                heapq.heappush(q, [D[u] + i[1], i[0]])
    return D

#コスト-最大値の最小を取っていくダイクストラ
def dijkstra2():
    D2[0][0] = 0
    q = []
    heapq.heappush(q, [0, 0]) #コスト、頂点
    while len(q) > 0:
        # ヒープから取り出し
        _, u = heapq.heappop(q)
        for i in E[u]:
            if D2[i[0]][1] > i[1]:
                if D2[i[0]][0] > D2[u][0] + i[1]:
                    D2[i[0]][0] = D2[u][0] + i[1]
                    heapq.heappush(q, [D2[i[0]][0], i[0]])
            else:
                if D2[i[0]][0] > D2[u][0] + D2[u][1]:
                    D2[i[0]][0] = D2[u][0] + D2[u][1]
                    D2[i[0]][1] = i[1]
                    heapq.heappush(q, [D2[i[0]][0], i[0]])

    return D2

dijkstra()
dijkstra2()
ans = [0] * N
for i in range(N):
    print(D[i] + D2[i][0])

0