#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int INF = numeric_limits::max()/4; //http://www.prefield.com/algorithm/graph/dijkstra.html typedef int Weight; struct Edge { int src, dst; Weight weight; Weight weight2; Edge(int src, int dst, Weight weight, Weight weight2) : src(src), dst(dst), weight(weight), weight2(weight2) { } }; bool operator < (const Edge &e, const Edge &f) { return make_pair(e.weight, e.weight2) > make_pair(f.weight, f.weight2); // !!INVERSE!! } typedef vector Edges; typedef vector Graph; typedef vector Array; typedef vector Matrix; void shortestPath(const Graph &g, int s, vector &dist, vector &prev, int C) { int n = g.size(); dist.assign(n, INF); dist[s] = 0; prev.assign(n, -1); priority_queue Q; // "e < f" <=> "e.weight > f.weight" for (Q.push(Edge(-2, s, 0, 0)); !Q.empty();) { Edge e = Q.top(); Q.pop(); if (prev[e.dst] != -1) continue; prev[e.dst] = e.src; for (const auto& f : g[e.dst]) { if (dist[f.dst] > e.weight + f.weight && C >= e.weight2 + f.weight2) { dist[f.dst] = e.weight + f.weight; Q.push(Edge(f.src, f.dst, e.weight + f.weight, e.weight2 + f.weight2)); } } } } int main() { Graph G; int N, C, V; { cin >> N >> C >> V; vector S(V), T(V), Y(V), M(V); for (int i = 0; i < V; i++) { cin >> S[i]; S[i]--; } for (int i = 0; i < V; i++) { cin >> T[i]; T[i]--; } for (int i = 0; i < V; i++) { cin >> Y[i]; } for (int i = 0; i < V; i++) { cin >> M[i]; } G.assign(N, vector()); for (int i = 0; i < V; i++) { G[S[i]].push_back(Edge(S[i], T[i], M[i], Y[i])); } } vector dist; vector prev; shortestPath(G, 0, dist, prev, C); int ans = dist[N - 1]; cout << (ans == INF ? -1 : ans) << endl; return 0; }