結果

問題 No.807 umg tours
ユーザー tempura_pp
提出日時 2019-03-17 01:36:37
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 3,667 ms / 4,000 ms
コード長 816 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 185,344 KB
最終ジャッジ日時 2024-07-05 01:17:32
合計ジャッジ時間 34,938 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush
def solve() : 
    N, M = map(int, input().split())

    edge = [[] for i in range(2*N)]
    for i in range(M):
        a, b, c = map(int, input().split())
        a-=1
        b-=1
        edge[a].append((c, b))
        edge[b].append((c, a))
        edge[a+N].append((c, b+N))
        edge[b+N].append((c, a+N))
        edge[a].append((0, b+N))
        edge[b].append((0, a+N))

    dist=[float('inf')]*(2*N)
    dist[0]=dist[N]=0

    pq = []
    heappush(pq, (0,0))
    heappush(pq, (0,N))
    while pq:
        d, cur = heappop(pq)
        if d > dist[cur]: continue

        for cost, to in edge[cur]:
            if dist[to]>d+cost : 
                dist[to]=d+cost
                heappush(pq, (d+cost,to))
    for i in range(N) :
        print(dist[i]+dist[i+N])
solve()
0