結果

問題 No.1601 With Animals into Institute
ユーザー ygd.
提出日時 2021-07-31 13:33:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,138 ms / 3,000 ms
コード長 1,724 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 144,672 KB
最終ジャッジ日時 2024-09-16 09:29:50
合計ジャッジ時間 17,606 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heapify, heappop, heappush
import sys 
input = sys.stdin.buffer.readline

def main():
    N,M = map(int,input().split())
    G = [[] for _ in range(N)]
    for i in range(M):
        a,b,c,x = map(int,input().split())
        a -= 1; b -= 1
        G[a].append((c,b,x))
        G[b].append((c,a,x))
    

    d = dijkstra_heap2(N-1,G) #研究所を始点に各家まで必ず動物を一回通る。
    #print(d)
    for i in range(N-1):
        print(d[i+N])


def dijkstra_heap2(s,G):
    INF = float('inf')
    #S:start, V: node, E: Edge, G: Graph
    V = len(G)
    #d = [[INF,INF] for _ in range(V)] #0:動物を取っていない、1:動物を取っている。
    d = [INF for _ in range(V*2)] #0~V-1:0, V~2*V-1:1
    d[s] = 0
    PQ = []
    heappush(PQ,(0,s)) #距離、頂点、動物がいる場合は+V

    while PQ:
        c,v = heappop(PQ)
        if d[v] < c:
            continue
        d[v] = c
        if v >= V:
            oriv = v - V
        else:
            oriv = v
        for cost,u,uani in G[oriv]:
            if v >= V: #既に動物を取っている
                u += V #動物を取った頂点に変更
                if d[u] <= cost + d[v]: continue
                d[u] = cost + d[v]
                heappush(PQ,(d[u],u))
            else: #vani = 0
                if uani == 0:
                    if d[u] <= cost + d[v]: continue
                    d[u] = cost + d[v]
                    heappush(PQ,(d[u],u))
                else: #uani == 1
                    u += V
                    if d[u] <= cost + d[v]: continue
                    d[u] = cost + d[v]
                    heappush(PQ,(d[u],u))

    return d


if __name__ == '__main__':
    main()
0