結果

問題 No.1601 With Animals into Institute
ユーザー ygd.ygd.
提出日時 2021-07-31 13:33:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,443 ms / 3,000 ms
コード長 1,724 bytes
コンパイル時間 2,136 ms
コンパイル使用メモリ 86,744 KB
実行使用メモリ 147,436 KB
最終ジャッジ日時 2023-10-14 14:55:48
合計ジャッジ時間 24,226 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,076 KB
testcase_01 AC 76 ms
71,220 KB
testcase_02 AC 78 ms
70,856 KB
testcase_03 AC 166 ms
79,304 KB
testcase_04 AC 160 ms
79,244 KB
testcase_05 AC 168 ms
79,364 KB
testcase_06 AC 1,278 ms
144,308 KB
testcase_07 AC 1,361 ms
144,568 KB
testcase_08 AC 1,312 ms
145,236 KB
testcase_09 AC 1,362 ms
144,304 KB
testcase_10 AC 1,368 ms
146,500 KB
testcase_11 AC 1,314 ms
143,992 KB
testcase_12 AC 1,321 ms
144,088 KB
testcase_13 AC 1,353 ms
147,436 KB
testcase_14 AC 1,325 ms
143,960 KB
testcase_15 AC 1,340 ms
143,592 KB
testcase_16 AC 1,443 ms
145,692 KB
testcase_17 AC 1,370 ms
144,316 KB
testcase_18 AC 173 ms
79,576 KB
testcase_19 AC 173 ms
79,732 KB
testcase_20 AC 166 ms
79,080 KB
testcase_21 AC 169 ms
79,444 KB
testcase_22 AC 174 ms
79,324 KB
testcase_23 AC 175 ms
79,404 KB
testcase_24 AC 169 ms
79,420 KB
testcase_25 AC 167 ms
79,456 KB
testcase_26 AC 170 ms
79,248 KB
testcase_27 AC 76 ms
71,060 KB
testcase_28 AC 76 ms
71,276 KB
testcase_29 AC 77 ms
71,140 KB
testcase_30 AC 76 ms
71,184 KB
testcase_31 AC 77 ms
71,404 KB
testcase_32 AC 77 ms
71,132 KB
testcase_33 AC 76 ms
71,120 KB
testcase_34 AC 78 ms
71,288 KB
testcase_35 AC 77 ms
71,076 KB
testcase_36 AC 78 ms
71,072 KB
testcase_37 AC 77 ms
71,072 KB
testcase_38 AC 77 ms
71,272 KB
権限があれば一括ダウンロードができます

ソースコード

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