結果

問題 No.1 道のショートカット
ユーザー はむ吉🐹
提出日時 2016-05-27 19:31:49
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 37 ms / 5,000 ms
コード長 1,371 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-07-20 16:23:37
合計ジャッジ時間 2,488 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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