結果

問題 No.1601 With Animals into Institute
ユーザー SI1000-githubSI1000-github
提出日時 2022-03-24 07:57:28
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,092 bytes
コンパイル時間 189 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 223,916 KB
最終ジャッジ日時 2024-04-20 17:22:11
合計ジャッジ時間 7,446 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
53,376 KB
testcase_01 AC 52 ms
52,992 KB
testcase_02 AC 50 ms
53,504 KB
testcase_03 AC 180 ms
80,280 KB
testcase_04 AC 186 ms
80,352 KB
testcase_05 AC 186 ms
79,872 KB
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 202 ms
80,768 KB
testcase_19 AC 203 ms
81,024 KB
testcase_20 AC 213 ms
81,024 KB
testcase_21 AC 188 ms
80,512 KB
testcase_22 AC 200 ms
80,896 KB
testcase_23 AC 200 ms
79,872 KB
testcase_24 AC 206 ms
80,640 KB
testcase_25 AC 214 ms
81,024 KB
testcase_26 AC 199 ms
80,512 KB
testcase_27 AC 53 ms
53,248 KB
testcase_28 AC 55 ms
53,632 KB
testcase_29 AC 50 ms
53,248 KB
testcase_30 AC 51 ms
53,376 KB
testcase_31 AC 53 ms
53,504 KB
testcase_32 AC 51 ms
53,504 KB
testcase_33 AC 57 ms
53,376 KB
testcase_34 AC 55 ms
53,504 KB
testcase_35 AC 93 ms
53,376 KB
testcase_36 AC 53 ms
53,248 KB
testcase_37 AC 50 ms
53,248 KB
testcase_38 AC 51 ms
53,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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)
    c = [[float('inf')] * 2 for _ in range(num_node)]    #スタート地点以外の値は∞で初期化
    c[start_id][0] = 0     #スタートは0で初期化
    visit = [0]*num_node

    node_name = []
    heapq.heappush(node_name, [0,0, start_id])

    while len(node_name) > 0:
        #ヒープから取り出し
        _, ani, min_point = heapq.heappop(node_name)

        if visit[min_point] == 0:

            #経路の要素を各変数に格納することで,視覚的に見やすくする
            for factor in edges[min_point]:
                goal = factor[0]   #終点
                cost  = factor[1]   #コスト
                x = factor[2] # 動物がいるかどうか

                if ani == 1:
                    if c[min_point][1] + cost < c[goal][1]:
                        c[goal][1] = c[min_point][1] + cost
                        heapq.heappush(node_name, [c[goal][1], 1, goal])

                if ani == 0:
                    if x == 0:
                        if c[min_point][0] + cost < c[goal][0]:
                            c[goal][0] = c[min_point][0] + cost
                            if c[goal][0] < c[goal][1]:
                                heapq.heappush(node_name, [c[goal][0], 0, goal])
                    if x == 1:
                        if c[min_point][0] + cost < c[goal][1]:
                            c[goal][1] = c[min_point][0] + cost
                            heapq.heappush(node_name, [c[goal][1], 1, goal])

        if ani == 1:
            visit[min_point] = 1



    return c


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][1])
0