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