#include #include #include #include #include #include #include #include #include #include #include #include #include #include constexpr int MAX_INT = 2147483647; constexpr int MIN_INT = -2147483648; #define rep(n, a, b) for (int (n) = (a); (n) < (b); (n)++) #define rrep(n, a, b) for (int (n) = (a); (n) >= (b); (n)--) #define llrep(n, a, b) for (long long (n) = (a); (n) < (b); (n)++) #define llrrep(n, a, b) for (long long (n) = (a); (n) >= (b); (n)--) #define itrAll(x) (x).begin(), (x).end() #define inputvec(v) for (auto& x : v) {std::cin >> x;} using namespace std; using uint = unsigned int; using ushort = unsigned short; using byte = unsigned char; using ll = long long; using ull = unsigned long long; using ldouble = long double; using ipair = pair; using llpair = pair; template void chmin(T& v1, T v2) { if (v1 > v2) v1 = v2; } template void chmax(T& v1, T v2) { if (v1 < v2) v1 = v2; } class union_find { private: vector parent; public: union_find(size_t N) { parent = vector(N); for (size_t i = 0; i < N; i++) { parent[i] = i; } } int get_root(size_t x) { if (parent[x] == x) { return x; } else { return (parent[x] = get_root(parent[x])); } } bool unite(size_t x, size_t y) { auto rx = get_root(x), ry = get_root(y); if (rx == ry) { return false; } else { parent[rx] = ry; return true; } } bool has_same_parent(size_t x, size_t y) { return get_root(x) == get_root(y); } }; int N, M, X; struct comparer { bool operator()(tuple& l, tuple& r) { return (get<1>(r) * X + get<2>(r)) - (get<1>(l) * X + get<2>(l)) < 0; } }; int main() { cin >> N >> M >> X; map> cost; // { 移動にかかる時間, 通行料 } union_find uf(N); // 到達可能性の確認用 rep(_, 0, M) { int ui, vi, ci, ti; cin >> ui >> vi >> ci >> ti; ui--, vi--; ipair p = { ti,ci }; cost[ui][vi] = p; cost[vi][ui] = p; uf.unite(ui, vi); } if (!uf.has_same_parent(0, N - 1)) { // 到達不能 cout << -1 << endl; return 0; } set visited; priority_queue, vector>, comparer> Q; // { 位置, 経過時間, かかったお金 } Q.push({ 0,0LL,0LL }); while (Q.size()) { auto [now, elapsed, spend] = Q.top(); Q.pop(); if (visited.find(now) != visited.end()) { continue; } visited.insert(now); if (now == N - 1) { cout << (elapsed + (int)ceil((double)spend / X)) << endl; return 0; } if (cost.find(now) != cost.end()) { vector tmp(cost[now].size()); for (auto& [nxt, tc] : cost[now]) { if (visited.find(nxt) == visited.end()) { Q.push({ nxt, elapsed + tc.first, spend + tc.second }); } else { tmp.push_back(nxt); } } for (auto key : tmp) { // 計算時間短縮......? cost[now].erase(key); cost[key].erase(now); } } } return 0; }