結果

問題 No.1 道のショートカット
ユーザー warashi
提出日時 2015-02-08 17:31:55
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,229 bytes
コンパイル時間 491 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 817,956 KB
最終ジャッジ日時 2024-07-08 03:44:59
合計ジャッジ時間 6,724 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 4 MLE * 1 -- * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import functools
import operator


def memoize(fn):
    table = {}

    def func(*args):
        if args not in table:
            table[args] = fn(*args)
        return table[args]
    return func

N = int(input())
C = int(input())
V = int(input())
S = [int(s) for s in input().split()]
T = [int(t) for t in input().split()]
Y = [int(y) for y in input().split()]
M = [int(m) for m in input().split()]

EDGE = dict(zip(zip(S, T), zip(Y, M)))


@memoize
def path(town):
    if town == 1:
        return [[1]]
    road = [path(s) for s, t in EDGE.keys() if t == town]
    if len(road) < 1:
        return [[-1]]
    return [p + [town] for p in functools.reduce(operator.add, road)]


def cost(path):
    cost = 0
    for i in range(1, len(path)):
        if path[i - 1] < 0:
            continue
        road = EDGE[path[i - 1], path[i]]
        cost += road[0]
    return cost


def meter(path):
    meter = 0
    for i in range(1, len(path)):
        if path[i - 1] < 0:
            continue
        road = EDGE[path[i - 1], path[i]]
        meter += road[1]
    return meter


route = [meter(p) for p in path(N) if p[0] == 1 and cost(p) <= C]
if len(route) == 0:
    print(-1)
else:
    print(min(route))
0