#include using namespace std; #define rep(i, j) for(int i=0; i < (int)(j); i++) template istream& operator >> (istream &is , vector &v) { for(T &a : v) is >> a; return is; } struct Vertex { int index, cost, time; }; using State = Vertex; class Solver { public: bool solve() { int N, C, V; cin >> N >> C >> V; vector S(V), T(V), Y(V), M(V); cin >> S >> T >> Y >> M; vector> E(V); rep(i, V) { S[i]--; T[i]--; E[S[i]].push_back(Vertex{T[i], Y[i], M[i]}); E[T[i]].push_back(Vertex{S[i], Y[i], M[i]}); } auto comp = [=](const State &a, const State &b) { return a.time > b.time; }; priority_queue, decltype(comp)> que(comp); que.push(State{0, 0, 0}); vector visited(V); int ans = -1; while(que.size()) { State now = que.top(); que.pop(); if(now.index == N - 1) { ans = now.time; break; } if(visited[now.index]) continue; visited[now.index] = true; for(Vertex &nxt : E[now.index]) { if(not visited[nxt.index] and now.cost + nxt.cost <= C) { que.push(State{ nxt.index, now.cost + nxt.cost, now.time + nxt.time }); } } } cout << -1 << endl; return 0; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); Solver s; s.solve(); return 0; }