結果
| 問題 | No.1601 With Animals into Institute | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-03-24 22:41:51 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2,442 ms / 3,000 ms | 
| コード長 | 6,630 bytes | 
| コンパイル時間 | 172 ms | 
| コンパイル使用メモリ | 82,316 KB | 
| 実行使用メモリ | 202,112 KB | 
| 最終ジャッジ日時 | 2024-10-13 06:24:29 | 
| 合計ジャッジ時間 | 34,734 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 36 | 
ソースコード
#!/usr/bin/python
# -*- coding: utf-8 -*-
# import
from sys import stdin, setrecursionlimit
setrecursionlimit(10**8)
# def
def int_map():
    return map(int,input().split())
def int_list():
    return ['Null']+list(map(int,input().split()))
# 整数のリストのリスト:N+1行のリスト(各行のリストは同じ長さでなくてよい.)
def int_mtx(N):
    x = [['Null']+list(map(int, stdin.readline().split())) for _ in range(N)]
    x.insert(0,['Null'])
    return x
# 文字のリストのリスト:N+1行のリスト(各行のリストは同じ長さでなくてよい.)
# AAAAA
# BBBBB
# CCCCC
# のような標準入力を
# '0AAAAA'
# '0BBBBB'
# '0CCCCC'
# と受け取る
def str_mtx(N):
    x = ['0'+stdin.readline()[:-1] for _ in range(N)]
    x.insert(0,'0')
    return x
# 文字のリストのリスト:N+1行のリスト(各行のリストは同じ長さでなくてよい.)
# AAAAA XXXXX
# BBBBB YYYYY
# CCCCC ZZZZZ
# のような標準入力を
# [[['0'],[AAAAA],[XXXXX]],
#  [['0'],[BBBBB],[YYYYY]],
#  [['0'],[CCCCC],[ZZZZZ]] ]
# と受け取る
def str_list(N):
    x = [['0']+list(map(str, stdin.readline().split())) for _ in range(N)]
    x.insert(0,['0'])
    return x
# 高さH+1, 幅W+1, のゼロ行列の作成
def zero_mtx(H,W):
    x = [[0]*(W+1) for i in range(H+1)]
    return x
# リストをスペースで分割する(先頭を省く)
def print_space(l):
    return print(" ".join([str(x) for x in l[1:]]))
# ゼロインデックスの場合
# 整数のリストのリスト:N行のリスト(各行のリストは同じ長さでなくてよい.)
def int_mtx_0(N):
    x = [list(map(int, stdin.readline().split())) for _ in range(N)]
    return x
# ゼロインデックスの場合
# 文字のリストのリスト:N+1行のリスト(各行のリストは同じ長さでなくてよい.)
def str_mtx_0(N):
    x = [list(stdin.readline()[:-1]) for _ in range(N)]
    return x
# ゼロインデックスの場合
# リストをスペースで分割する(先頭を省かない)
def print_space_0(l):
    return print(" ".join([str(x) for x in l]))
# ゼロインデックスの場合
# 高さH, 幅W, のゼロ行列の作成
def zero_mtx_0(H,W):
    x = [[0]*W for i in range(H)]
    return x
def int_list_0():
    return list(map(int,input().split()))
## インポート
# from collections import deque
# 順列に使う
# import itertools
# 最大公約数などに使う
# import math
# リストの要素の数をdict形式で
# import collections
# 二次元配列のコピーを作りたいとき
# a_copy = deepcopy(a)
# from copy import deepcopy
# main code
N,M = int_map()
ABCX = int_mtx_0(M)
"""
ダイクストラ法(ヒープによる優先度付きキューを用いて)
計算量  O( |E|*log(|V|))   E:辺の数   V:頂点数
ベルマンフォードより早いが,負のコストがあると使えない.
"""
import heapq
def dijkstra(edges, start_id):
    num_node = len(edges)
    c0 = [float('INF')]*num_node
    c1 = [float('INF')]*num_node
    c0[start_id] = 0     #スタートは0で初期化
    visit = [0]*num_node
    candidate0 = []
    candidate1 = []
    heapq.heappush(candidate0, [0, start_id])
    while len(candidate0)>0 or len(candidate1)>0:
        if len(candidate0) == 0:
            _, min_point = heapq.heappop(candidate1)
            #経路の要素を各変数に格納することで,視覚的に見やすくする
            for factor in edges[min_point]:
                goal = factor[0]   #終点
                cost  = factor[1]   #コスト
                x = factor[2] # 動物がいるかどうか
                if c1[min_point] + cost < c1[goal]:
                    c1[goal] = c1[min_point] + cost
                    heapq.heappush(candidate1, [c1[goal], goal])
        elif len(candidate1) == 0:
            _, min_point = heapq.heappop(candidate0)
            #経路の要素を各変数に格納することで,視覚的に見やすくする
            for factor in edges[min_point]:
                goal = factor[0]   #終点
                cost  = factor[1]   #コスト
                x = factor[2] # 動物がいるかどうか
                if x == 0:
                    if c0[min_point] + cost < c0[goal]:
                        c0[goal] = c0[min_point] + cost
                        heapq.heappush(candidate0, [c0[goal], goal])
                else:
                    if c0[min_point] + cost < c1[goal]:
                        c1[goal] = c0[min_point] + cost
                        heapq.heappush(candidate1, [c1[goal], goal])
        else:
            if candidate0[0][0] <= candidate1[0][0]:
                _, min_point = heapq.heappop(candidate0)
                #経路の要素を各変数に格納することで,視覚的に見やすくする
                for factor in edges[min_point]:
                    goal = factor[0]   #終点
                    cost  = factor[1]   #コスト
                    x = factor[2] # 動物がいるかどうか
                    if x == 0:
                        if c0[min_point] + cost < c0[goal]:
                            c0[goal] = c0[min_point] + cost
                            heapq.heappush(candidate0, [c0[goal], goal])
                    else:
                        if c0[min_point] + cost < c1[goal]:
                            c1[goal] = c0[min_point] + cost
                            heapq.heappush(candidate1, [c1[goal], goal])
            else:
                _, min_point = heapq.heappop(candidate1)
                #経路の要素を各変数に格納することで,視覚的に見やすくする
                for factor in edges[min_point]:
                    goal = factor[0]   #終点
                    cost  = factor[1]   #コスト
                    x = factor[2] # 動物がいるかどうか
                    if c1[min_point] + cost < c1[goal]:
                        c1[goal] = c1[min_point] + cost
                        heapq.heappush(candidate1, [c1[goal], goal])
    return c1
start_id = N-1
# UVC = [ [始点1,終点1,コスト1], ..., [始点M,終点M,コストM] ]
UVC = []
for i in range(M):
    UVC.append([ABCX[i][0]-1, ABCX[i][1]-1, ABCX[i][2], ABCX[i][3]])
Di = False
Edges = [[] for _ in range(N)]
if Di:
    for ui, vi, ci in UVC:
        Edges[ui].append([vi,ci])
else:
    for ui, vi, ci, xi in UVC:
        Edges[ui].append([vi,ci,xi])
        Edges[vi].append([ui,ci,xi])
opt = dijkstra(Edges, start_id)
for i in range(N-1):
    print(opt[i])
            
            
            
        