結果

問題 No.1 道のショートカット
コンテスト
ユーザー はむ吉🐹
提出日時 2016-05-27 19:31:49
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 103 ms / 5,000 ms
コード長 1,371 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 361 ms
コンパイル使用メモリ 20,700 KB
実行使用メモリ 35,932 KB
最終ジャッジ日時 2026-04-03 01:22:10
合計ジャッジ時間 6,042 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#!/usr/bin/env python3

import collections
import heapq


IMPOSSIBLE = -1
Edge = collections.namedtuple("Edge", "start end money time")
AdjVertex = collections.namedtuple("AdjVertex", "dest, money, time")
State = collections.namedtuple("State", "time money vertex")


def compute_min_time(num_vertices, money_limit, adj_vertices):
    pq = []
    heapq.heappush(pq, State(0, 0, 0))
    while pq:
        state0 = heapq.heappop(pq)
        if state0.vertex == num_vertices - 1:
            return state0.time
        for v in adj_vertices[state0.vertex]:
            new_money = state0.money + v.money
            if new_money <= money_limit:
                new_time = state0.time + v.time
                heapq.heappush(pq, State(new_time, new_money, v.dest))
    else:
        return IMPOSSIBLE


def main():
    num_vertices = int(input())
    money_limit = int(input())
    _ = input()
    ss = map(lambda x: int(x) - 1, input().split())
    ts = map(lambda x: int(x) - 1, input().split())
    ys = map(int, input().split())
    ms = map(int, input().split())
    adj_vertices = [set() for _ in range(num_vertices)]
    for edge in (Edge(*args) for args in zip(ss, ts, ys, ms)):
        adj_vertices[edge.start].add(AdjVertex(*edge[1:]))
    answer = compute_min_time(num_vertices, money_limit, adj_vertices)
    print(answer)


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