結果

問題 No.1 道のショートカット
ユーザー warashiwarashi
提出日時 2015-02-08 22:16:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,854 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 11,044 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-22 11:56:45
合計ジャッジ時間 2,672 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,080 KB
testcase_01 AC 19 ms
8,152 KB
testcase_02 AC 21 ms
8,096 KB
testcase_03 AC 20 ms
8,104 KB
testcase_04 AC 18 ms
8,160 KB
testcase_05 AC 17 ms
8,028 KB
testcase_06 AC 18 ms
8,080 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 21 ms
8,596 KB
testcase_10 AC 23 ms
8,680 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 18 ms
8,100 KB
testcase_16 WA -
testcase_17 AC 19 ms
8,100 KB
testcase_18 AC 20 ms
8,096 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 22 ms
8,628 KB
testcase_24 AC 20 ms
8,524 KB
testcase_25 AC 18 ms
8,152 KB
testcase_26 AC 19 ms
8,196 KB
testcase_27 WA -
testcase_28 AC 19 ms
8,072 KB
testcase_29 WA -
testcase_30 AC 19 ms
8,152 KB
testcase_31 AC 19 ms
8,492 KB
testcase_32 AC 20 ms
8,588 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 20 ms
8,632 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 18 ms
8,052 KB
testcase_43 AC 17 ms
8,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
def memoize(fn):
    table = {}

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


class Town(object):

    def __init__(self, num):
        self.edge = []
        self.num = num
        self.is_start = False
        self.is_goal = False

    def append(self, edge):
        self.edge.append(edge)


class Edge(object):

    def __init__(self, frm, to, cost, distance):
        self.frm = frm
        self.to = to
        self.cost = cost
        self.distance = distance
        self.used = False


def forward(walk, now, yen):
    path = [e for e in now.edge if not e.used and yen >= e.cost]
    if len(path) == 0:
        return (walk, now, yen, True)
    edge = min(path, key=lambda x: (x.distance / (x.to.num - now.num)))
    edge.used = True
    walk.append(edge)
    now = edge.to
    yen -= edge.cost
    return (walk, now, yen, False)


def back(walk, now, yen):
    if now.is_start:
        return (walk, now, yen, True)
    for e in now.edge:
        e.used = False
    e = walk.pop()
    now = e.frm
    yen -= e.cost
    return (walk, now, yen, False)

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()]

town = [Town(num) for num in range(N)]
RODE = zip(S, T, Y, M)
for s, t, y, m in RODE:
    town[s - 1].append(Edge(town[s - 1], town[t - 1], y, m))
town[0].is_start = True
town[-1].is_goal = True

walk = []
now = town[0]
yen = C
while not now.is_goal:
    walk, now, yen, err = forward(walk, now, yen)
    if err:
        walk, now, yen, err = back(walk, now, yen)
        if err:
            print(-1)
            break
if now.is_goal:
    print(sum([e.distance for e in walk]))
0