#include //#include //#include //#include using namespace std; // Macro and Macro Functions #define rep(a, b) for(int a = 0; a < b; ++a) #define REP(a, b, c) for(int a = b; a < c; ++a) #define drep(a, b, c) for(int a=0,b=0;b>a[nanika] #define ALLINDE(a) rep(kyawa,a.size()){cin>>a[kyawa]; --a[kyawa];} #define YN(a) do{if(a) cout<<"YES"; else cout<<"NO";}while(0) #define Yn(a) do{if(a) cout<<"Yes"; else cout<<"No";}while(0) #define yn(a) do{if(a) cout<<"yes"; else cout<<"no";}while(0) #define spa " " #define dspa " " #define ctoi(c) (c)-'0' #define altoi(a) (a)-'a' #define fi first #define se second #define dvec(type,name,row,column,value) vector> name(row,vector(column,value)) // List of using using ll = signed long long; using ull = unsigned long long; using pint = pair; using pong = pair; using tint = tuple; using tong = tuple; using vint = vector; using vll = vector; using vbol = vector; using vstr = vector; using vull = vector; using dvin = vector>; using dvbo = vector>; using mint = map; // Common Variable bool DEBUG=false; unsigned long long INF=(1<<31); void print(){ cout<<'\n'; } template void print(HEAD&& head,TAIL&&... tail){ cout<(tail)...); } template void dprint(HEAD&& head,TAIL&&... tail){ cout<<"Debug : "<(tail)...); } void scan(){} template void scan(HEAD&& head,TAIL&&... tail){ cin>>head; scan(forward(tail)...); } template void print(vector &v){ rep(i,v.size()){ cout< void print(vector> &v){ rep(i,v.size()){ cout< struct edge{int to; T cost,tt;}; template class Graph{ public : int size; vector>> Graph; vector dis; vector prev; vector> Tdis; void init(int n){ size=n; Graph.resize(size); dis.resize(size); prev.resize(size); } void add(int x,int y,T z,T zz){ Graph[x].emplace_back((edge){y,z,zz}); } }; //-------------------------------- // int dx[]={ 1, 0,-1, 0}; // int dy[]={ 0,-1, 0, 1}; signed main(){ int n,c,v; cin>>n>>c>>v; vint s(v),t(v),y(v),m(v); ALLINDE(s); ALLINDE(t); ALLIN(y); ALLIN(m); Graph gr; gr.init(n); rep(i,v){ gr.add(s[i],t[i],y[i],m[i]); } // 現在位置、現在の必要経費、現在の経過時間 queue q; int ans=(1<<30); q.emplace(0,0,0); while(!q.empty()){ int tp,tc,tt; tie(tp,tc,tt)=q.front(); q.pop(); //print(tp+1,tc,tt); if(tp==n-1){ ans=min(ans,tt); continue; } rep(i,gr.Graph[tp].size()){ if(tc+gr.Graph[tp][i].cost>c) continue; q.emplace(gr.Graph[tp][i].to,tc+gr.Graph[tp][i].cost,tt+gr.Graph[tp][i].tt); } } if(ans==(1<<30)) cout<<-1<