#include #define rep(i, n) for(int i=0, i##_len=(n); i=0; --i) #define rreps(i, n) for(int i=((int)(n)); i>0; --i) #define all(v) (v).begin(), (v).end() using namespace std; using ll = long long; using ull = unsigned long long; using vi = vector; using vvi = vector>; using vvvi = vector>>; using vl = vector; using vvl = vector>; using vvvl = vector>>; using vs = vector; using pi = pair; using pl = pair; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (bbool chmaxeq(T &a, const T &b) { if (a<=b) { a=b; return 1; } return 0; } templatebool chmineq(T &a, const T &b) { if (b<=a) { a=b; return 1; } return 0; } bool yes(bool a) { cout << (a?"yes":"no") << endl; return a; } bool Yes(bool a) { cout << (a?"Yes":"No") << endl; return a; } bool YES(bool a) { cout << (a?"YES":"NO") << endl; return a; } void _main(); int main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(16); _main(); return 0; } struct UnionFind { vector d; UnionFind(int n=0): d(n,-1) {} int find(int x) { if (d[x] < 0) return x; return d[x] = find(d[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (d[x] > d[y]) swap(x,y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return find(x) == find(y);} int size(int x) { return -d[find(x)];} }; void _main() { ll N, M, X; cin >> N >> M >> X; vector>> g(N); UnionFind uf(N); rep(i, M) { ll u, v, C, T; cin >> u >> v >> C >> T; u--; v--; g[u].push_back({v, {C, T}}); g[v].push_back({u, {C, T}}); uf.unite(u, v); } if (!uf.same(0, N-1)) { cout << -1 << endl; return; } g[0].push_back({0, {-X, 1}}); vector> d(N); priority_queue, greater> q; d[0][0] = 0; q.push({0, 0}); ll ans = 1LL << 60; while (!q.empty() && q.top().first < ans) { pl p = q.top(); q.pop(); ll m = d[p.second][p.first]; for (auto &x : g[p.second]) { if (m-x.second.first < 0) continue; if (chmax(d[x.first][p.first+x.second.second], m-x.second.first)) { q.push({p.first+x.second.second, x.first}); if (x.first == N-1) chmin(ans, p.first+x.second.second); } } } cout << ans << endl; }