#include #include #include #include #include #include #include #include #include #include static const int MOD = 1000000007; using ll = long long; using u32 = uint32_t; using namespace std; template constexpr T INF = ::numeric_limits::max()/32*15+208; template vector make_v(U size, const T& init){ return vector(static_cast(size), init); } template auto make_v(U size, Ts... rest) { return vector(static_cast(size), make_v(rest...)); } template void chmin(T &a, const T &b){ a = (a < b ? a : b); } template void chmax(T &a, const T &b){ a = (a > b ? a : b); } template struct edge { int from, to; T cost; edge(int to, T cost) : from(-1), to(to), cost(cost) {} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} }; template vector dijkstra(int s,vector>> &G){ auto n = G.size(); vector d(n, INF); priority_queue,vector>,greater<>> Q; d[s] = 0; Q.emplace(0, s); while(!Q.empty()){ T cost; int i; tie(cost, i) = Q.top(); Q.pop(); if(d[i] < cost) continue; for (auto &&e : G[i]) { auto cost2 = cost + e.cost; if(d[e.to] <= cost2) continue; d[e.to] = cost2; Q.emplace(d[e.to], e.to); } } return d; } template using GPQ = priority_queue, greater>; int main() { int n, c, v; cin >> n >> c >> v; auto dp = make_v(n, c+1, INF); dp[0][0] = 0; using P = array; vector> G(n); vector s(v), t(v), y(v), m(v); for (auto &&i : s) scanf("%d", &i); for (auto &&i : t) scanf("%d", &i); for (auto &&i : y) scanf("%d", &i); for (auto &&i : m) scanf("%d", &i); for (int i = 0; i < v; ++i) { G[s[i]-1].push_back(P{t[i]-1, y[i], m[i]}); } GPQ> Q; Q.emplace(0, 0, 0); while(!Q.empty()){ int i, j, cost; tie(i, j, cost) = Q.top(); Q.pop(); if(dp[i][j] < cost) continue; for (auto &&e : G[i]) { if(j+e[1] > c) continue; auto cost2 = cost + e[2]; if(dp[e[0]][j+e[1]] <= cost2) continue; dp[e[0]][j+e[1]] = cost2; Q.emplace(e[0], j+e[1], dp[e[0]][j+e[1]]); } } int ans = *min_element(dp.back().begin(),dp.back().end()); cout << (ans == INF ? -1 : ans) << "\n"; return 0; }