#include // cout, endl, cin #include // string, to_string, stoi #include // vector #include // min, max, swap, sort, reverse, lower_bound, upper_bound #include // pair, make_pair #include // tuple, make_tuple #include // int64_t, int*_t #include // printf #include // map #include // queue, priority_queue #include // set #include // stack #include // deque #include // unordered_map #include // unordered_set #include // bitset #include // isupper, islower, isdigit, toupper, tolower #include #include #include #include #include #include #include #include #include //#include //using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define repl(i,l,r) for (int i = l; i < (int)(r); i++) #define all(a) a.begin(),a.end() #define Pii pair #define Pll pair #define INFi 1000000001 #define INFl 1000000000000000001 #define ll long long using namespace std; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template void printArray(vector&A){ for(T&a:A){ cout< void printArrayln(vector&A){ for(T&a:A){ cout< std::ostream &operator<<(std::ostream &out, const pair &A){ cout<<"{"< std::ostream &operator<<(std::ostream &out, const map &M){ for(const auto&A:M){ cout<<"{"< std::ostream &operator<<(std::ostream &out, const set &M){ cout<<"{"; for(const auto&A:M){ cout< std::ostream &operator<<(std::ostream &out, const multiset &M){ cout<<"{"; for(const auto&A:M){ cout< std::ostream &operator<<(std::ostream &out, const vector &A){ for(const T &a:A){ cout< void print(Head H, Tail... T) { cout << H << " "; print(T...); } template std::istream &operator>>(std::istream &in,vector&A){ for(T&a:A){ std::cin>>a; } return in; } struct Edge{ int to; long cost; long time; }; using Graph = vector>; struct State{ int v; long cost; long time; bool operator>(const State &s) const{ if(time==s.time){ return costs.time; } }; int main(void){ std::cin.tie(0)->sync_with_stdio(0); int N,M,X;cin>>N>>M>>X; Graph G(N); rep(i,M){ int a,b,c,t;cin>>a>>b>>c>>t; a--;b--; G[a].push_back({b,c,t}); G[b].push_back({a,c,t}); } priority_queue,greater> pq; pq.push({0,0,0}); vector dist(N,{0,INFl,INFl}); dist[0]={0,0,0}; int cnt = 0; while(!pq.empty() && cnt < 300000){ State s=pq.top();pq.pop(); if(s.v == N-1){ break; } for(Edge e:G[s.v]){ cnt++; long t = s.time+e.time; long m = s.cost - e.cost; if(m<0){ // お金が足りない long m2 = abs(m); //あとm円足りない long h = (m2+X-1)/X; //h時間働いてm円稼ぐ t+=h; m+=h*X; } State next = {e.to,m,t}; if(dist[e.to]>next){ dist[e.to]=next; pq.push(next); } } } if(dist[N-1].time==INFl){ cout<<-1<