#include #include using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i; using vvi = vector>; template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline void drop(T x) { cout << x << endl; exit(0); } template void vout(vector v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; using pint = pair; using vp = vector; using tint = tuple; using vt = vector; vector>> G; void make_graph(lint N, lint M) { lint x, y, a, b; G.resize(N); rep(i, M) { cin >> x >> y >> a >> b; x--, y--; G[x].push_back(make_tuple(a, b, y)); G[y].push_back(make_tuple(a, b, x)); } } void dijkstra(lint start, vector &dist, lint W) { lint a=0, b=0, c=0, x, y, z, N = G.size(); dist.resize(N, LINF); dist[start] = 0; priority_queue, greater> PQ; PQ.push(make_pair(0, start)); while (!PQ.empty()) { tie(b, a) = PQ.top(); PQ.pop(); if (dist[a] < b) continue; for (auto p : G[a]) { tie(y, z, x) = p; if (z >= W && chmin(dist[x], b+y)) PQ.push(make_pair(b+y, x)); } } } int main() { lint N, M, X; cin >> N >> M >> X; make_graph(N, M); lint a=0, b=LINF, c=0, x, y, z; while (b-a > 1) { c = (a+b)/2; vi dist; dijkstra(0, dist, c); if (dist[N-1] <= X) a = c; else b = c; } if (!a) std::cout << -1 << '\n'; else std::cout << a << '\n'; }