#define _USE_MATH_DEFINES #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include //#include using namespace std; //using namespace atcoder; #define LP(I,S,G) for (long long int I = (S); I < (G); ++I) #define IN(X) for (int in = 0; in < X.size(); in++)cin >> X[in] #define OUT(X) for (int in = 0; in < X.size(); in++)cout << X[in]<<" " #define SORT(X) sort((X).begin(), (X).end()) #define CSORT(X,Y) sort(X.begin(), X.end(),Y) #define COPY(X,Y) copy(X.begin(), X.end(), Y.begin()) #define ALL(X,Y) for (auto &(X) :(Y)) #define FULL(a) (a).begin(),(a).end() #define BFS(Q,S) for(Q.push(S);Q.size()!=0;Q.pop()) typedef long long int ll; typedef unsigned long long int ull; long long int M = 998244353; chrono::system_clock::time_point starttime; using namespace std::chrono; inline float getTime() { return duration_cast(system_clock::now() - starttime).count(); } int dx[] = { -1,0,1,0 }, dy[] = { 0,1,0,-1 }; ll MAX(ll A, ll B) { return ((A) > (B) ? (A) : (B)); } ll MIN(ll A, ll B) { return ((A) < (B) ? (A) : (B)); } inline long long int xor128() { static long long int x = 123456789, y = 362436069, z = 521288629, w = 88675123; long long int t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } int main() { ll n, m, x; cin >> n >> m >> x; vector>> g(n); LP(i, 0, 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 }); } function, array)> f= [x](array a, array b) { pair l = { a[0] + (a[1] + x - 1) / x,a[2] }; pair r = { b[0] + (b[1] + x - 1) / x,b[2] }; return l < r; }; priority_queue,vector>, function, array)>>pq(f); pq.push({ 0,0,0 }); vector> d(n, { -1,-1 }); d[0] = { 0,0 }; while (pq.size() != 0) { auto[nd, nc, now] = pq.top(); pq.pop(); if (nd != d[now][0] || nc != d[now][1])continue; ALL(next, g[now]) { if (d[next[0]][0] == -1) { d[next[0]] = { nd + next[2],nc + next[1] }; pq.push({ nd + next[2],nc + next[1],next[0] }); } else { ll bc = d[next[0]][0] + (d[next[0]][1] + x - 1) / x; ll cp = nd + next[2] + (nd + next[1] + x - 1) / x; if (bc > cp) { d[next[0]] = { nd + next[2],nc + next[1] }; pq.push({ nd + next[2],nc + next[1],next[0] }); } } } } if (d[n - 1][0] == -1)cout << -1; else { ll bc = d[n - 1][0] + (d[n - 1][1] + x - 1) / x; cout << bc; } return 0; }