結果

問題 No.1601 With Animals into Institute
ユーザー lam6er
提出日時 2025-03-20 19:00:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,303 ms / 3,000 ms
コード長 1,572 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 190,908 KB
最終ジャッジ日時 2025-03-20 19:01:11
合計ジャッジ時間 31,521 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    M = int(input[idx])
    idx += 1
    
    adj = [[] for _ in range(N+1)]
    for _ in range(M):
        A = int(input[idx])
        idx += 1
        B = int(input[idx])
        idx += 1
        C = int(input[idx])
        idx += 1
        X = int(input[idx])
        idx += 1
        adj[A].append((B, C, X))
        adj[B].append((A, C, X))
    
    INF = 1 << 60
    d0 = [INF] * (N + 1)
    d1 = [INF] * (N + 1)
    d0[N] = 0
    heap = []
    heapq.heappush(heap, (0, N, 0))
    
    while heap:
        dist, u, state = heapq.heappop(heap)
        if state == 0:
            if dist > d0[u]:
                continue
        else:
            if dist > d1[u]:
                continue
        
        for (v, c, x) in adj[u]:
            if state == 0:
                new_dist0 = d0[u] + c
                if x == 1:
                    if new_dist0 < d1[v]:
                        d1[v] = new_dist0
                        heapq.heappush(heap, (d1[v], v, 1))
                else:
                    if new_dist0 < d0[v]:
                        d0[v] = new_dist0
                        heapq.heappush(heap, (d0[v], v, 0))
            if state == 1:
                new_dist1 = d1[u] + c
                if new_dist1 < d1[v]:
                    d1[v] = new_dist1
                    heapq.heappush(heap, (d1[v], v, 1))
    
    for i in range(1, N):
        print(d1[i] if d1[i] != INF else -1)

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