/* -*- coding: utf-8 -*- * * 2387.cc: No.2387 Yokan Factory - yukicoder */ #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_M = 100000; const long long LINF = 1LL << 62; /* typedef */ typedef long long ll; typedef pair pii; typedef vector vpii; typedef pair pli; /* global variables */ vpii nbrs[MAX_N]; int as[MAX_M], bs[MAX_M]; ll ds[MAX_N]; /* subroutines */ ll dijkstra(int n, int b) { fill(ds, ds + n, LINF); ds[0] = 0; priority_queue q; q.push(pli(0, 0)); while (! q.empty()) { auto ue = q.top(); q.pop(); ll ud = -ue.first; int u = ue.second; if (ds[u] != ud) continue; if (u == n - 1) break; for (auto ve: nbrs[u]) { int v = ve.first, ei = ve.second; if (bs[ei] >= b) { ll vd = ud + as[ei]; if (ds[v] > vd) { ds[v] = vd; q.push(pli(-vd, v)); } } } } return ds[n - 1]; } /* main */ int main() { int n, m; ll x; scanf("%d%d%lld", &n, &m, &x); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d%d%d", &u, &v, as + i, bs + i); u--, v--; nbrs[u].push_back(pii(v, i)); nbrs[v].push_back(pii(u, i)); } if (dijkstra(n, 1) > x) { puts("-1"); return 0; } int maxb = *max_element(bs, bs + m); int b0 = 1, b1 = maxb + 1; while (b0 + 1 < b1) { int b = (b0 + b1) / 2; if (dijkstra(n, b) > x) b1 = b; else b0 = b; } printf("%d\n", b0); return 0; }