#include #include #include #include #include using namespace std; #define INF 10000000 typedef pair < unsigned, unsigned > P; class edge{ public: unsigned to; unsigned cost; unsigned time; edge(unsigned a, unsigned b, unsigned c) { to = a; cost = b; time = c; } }; int main(void) { unsigned N = 0, C = 0, V = 0; unsigned S[1500] = {0}, T[1500] = {0}, Y[1500] = {0}, M[1500] = {0}; vector G[51]; priority_queue

, greater

> que; unsigned time[51], money[51] = {0}; cin >> N >> C >> V; for(unsigned i = 0; i < V; i++){ cin >> S[i]; } for(unsigned i = 0; i < V; i++){ cin >> T[i]; } for(unsigned i = 0; i < V; i++){ cin >> Y[i]; } for(unsigned i = 0; i < V; i++){ cin >> M[i]; } for(unsigned i = 0; i < V; i++){ edge e(T[i], Y[i], M[i]); G[S[i]].push_back(e); } for(unsigned i = 1; i <= N; i++){ time[i] = INF; } que.push(P(1,0)); time[1] = 0; money[1] = 0; while(!que.empty()){ P p = que.top(); que.pop(); unsigned pos = p.first, t = p.second; if(time[pos] < t){ continue; } for(unsigned i = 1; i <= N; i++){ for(auto e : G[pos]){ if(time[e.to] > time[pos] + e.time && C>=money[pos]+e.cost){ money[e.to] = money[pos] + e.cost; time[e.to] = time[pos] + e.time; que.push(P(e.to,time[e.to])); } } } } if(time[N] != INF){ cout << time[N] << endl; } else{ cout << -1 << endl; } return 0; }