結果

問題 No.807 umg tours
コンテスト
ユーザー koheijkt
提出日時 2026-05-17 11:44:54
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 2,023 ms / 4,000 ms
コード長 1,488 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 263 ms
コンパイル使用メモリ 85,120 KB
実行使用メモリ 166,764 KB
最終ジャッジ日時 2026-05-17 11:45:16
合計ジャッジ時間 20,346 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

INF = 1e18
from collections import deque
from heapq import heappush, heappop
N, M = map(int, input().split())
G = [list() for _ in range(N)]
for i in range(M):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append((v, w))
    G[v].append((u, w))

# 頂点0からの距離
dist = [[INF] * (2) for _ in range(N)]
dist[0][0] = 0
dist[0][1] = 0

kakutei = [[False] * (2) for _ in range(N)]
que = []
heappush(que, (0, 0, 0))
while que:
    curr_dist, pos, mode = heappop(que)
    if kakutei[pos][mode]:
        continue
    kakutei[pos][mode] = True

    if mode == 0:
        # mode 0 のときは
        # 0 to 0
        for nex, kyori in G[pos]:
            if kakutei[nex][0]:
                continue
            if dist[nex][0] > curr_dist + kyori:
                dist[nex][0] = curr_dist + kyori
                heappush(que, (dist[nex][0], nex, 0))
        # 0 to 1
        for nex, kyori in G[pos]:
            if kakutei[nex][1]:
                continue
            if dist[nex][1] > curr_dist:
                dist[nex][1] = curr_dist
                heappush(que, (dist[nex][1], nex, 1))
    else:
        # mode: 1 のときは普通のダイクストラ
        for nex, kyori in G[pos]:
            if kakutei[nex][1]:
                continue
            if dist[nex][1] > curr_dist + kyori:
                dist[nex][1] = curr_dist + kyori
                heappush(que, (dist[nex][1], nex, 1))
#print(dist)
for i in range(N):
    print(sum(dist[i]))
0