結果

問題 No.1601 With Animals into Institute
ユーザー ygd.ygd.
提出日時 2021-07-31 13:25:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,051 ms / 3,000 ms
コード長 1,559 bytes
コンパイル時間 357 ms
コンパイル使用メモリ 86,700 KB
実行使用メモリ 169,140 KB
最終ジャッジ日時 2023-10-14 14:55:22
合計ジャッジ時間 32,101 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,076 KB
testcase_01 AC 62 ms
71,256 KB
testcase_02 AC 66 ms
71,024 KB
testcase_03 AC 235 ms
79,548 KB
testcase_04 AC 140 ms
79,636 KB
testcase_05 AC 147 ms
79,912 KB
testcase_06 AC 1,973 ms
166,364 KB
testcase_07 AC 1,937 ms
165,032 KB
testcase_08 AC 1,936 ms
166,544 KB
testcase_09 AC 1,901 ms
164,176 KB
testcase_10 AC 2,051 ms
169,140 KB
testcase_11 AC 1,899 ms
165,508 KB
testcase_12 AC 1,878 ms
164,268 KB
testcase_13 AC 1,884 ms
166,580 KB
testcase_14 AC 1,779 ms
164,056 KB
testcase_15 AC 1,914 ms
164,360 KB
testcase_16 AC 1,944 ms
164,948 KB
testcase_17 AC 1,799 ms
164,716 KB
testcase_18 AC 154 ms
79,944 KB
testcase_19 AC 155 ms
80,684 KB
testcase_20 AC 147 ms
79,712 KB
testcase_21 AC 146 ms
79,932 KB
testcase_22 AC 149 ms
79,500 KB
testcase_23 AC 149 ms
79,924 KB
testcase_24 AC 153 ms
79,812 KB
testcase_25 AC 146 ms
79,648 KB
testcase_26 AC 151 ms
79,964 KB
testcase_27 AC 63 ms
71,260 KB
testcase_28 AC 66 ms
71,272 KB
testcase_29 AC 63 ms
71,556 KB
testcase_30 AC 63 ms
71,228 KB
testcase_31 AC 66 ms
71,456 KB
testcase_32 AC 64 ms
71,460 KB
testcase_33 AC 65 ms
71,168 KB
testcase_34 AC 63 ms
71,228 KB
testcase_35 AC 62 ms
71,072 KB
testcase_36 AC 65 ms
71,456 KB
testcase_37 AC 61 ms
71,584 KB
testcase_38 AC 61 ms
71,256 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][1])


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[s][0] = 0
    PQ = []
    heappush(PQ,(0,s,0)) #距離、頂点、動物

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

    return d


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