結果

問題 No.1601 With Animals into Institute
ユーザー rlangevin
提出日時 2023-02-25 19:38:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,191 ms / 3,000 ms
コード長 1,381 bytes
コンパイル時間 819 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 142,456 KB
最終ジャッジ日時 2024-09-13 14:56:49
合計ジャッジ時間 20,369 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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, X = map(int, readline().split())
    A, B = A - 1, B - 1
    if X:
        G[A].append((B + N, C))
        G[B].append((A + N, C))
        G[A + N].append((B + N, C))
        G[B + N].append((A + N, C))
    else:
        G[A].append((B, C))
        G[B].append((A, C))
        G[A + N].append((B + N, C))
        G[B + N].append((A + N, C))
        
D = dijkstra(N - 1, -1, 2 * N)
for i in range(N - 1):
    print(D[i + N])
0