#include #include #include #include #include #include #include using namespace std; template using pq = priority_queue, greater>; int main(){ long long N, C, V, cost, mi=1e18; long long from, alt, d; cin >> N >> C >> V; pq> que; vector>> E(N); vector> dist(N, vector(C+1, 1e18)); vector> visit(N, vector(C+1)); vector S(V), T(V), Y(V), M(V); for (int i=0; i> S[i]; for (int i=0; i> T[i]; for (int i=0; i> Y[i]; for (int i=0; i> M[i]; for (int i=0; i < V; i++){ E[S[i]-1].push_back({T[i]-1, M[i], Y[i]}); } dist[0][0] = 0; //dist, from, cost; que.push({0, 0, 0}); while(!que.empty()){ tie(d, from, cost) = que.top(); que.pop(); if (visit[from][cost]) continue; visit[from][cost] = 1; for (auto [t, m, y] : E[from]){ alt = d + m; if (cost+y <= C && alt < dist[t][cost+y]){ dist[t][cost+y] = alt; que.push({alt, t, cost+y}); } } } for (int i=0; i<=C; i++) mi = min(mi, dist[N-1][i]); cout << (mi ==1e18 ? -1 : mi) << endl; return 0; }