#![allow(non_snake_case)] #![allow(unused_imports)] #![allow(unused_macros)] #![allow(clippy::needless_range_loop)] #![allow(clippy::comparison_chain)] #![allow(clippy::nonminimal_bool)] #![allow(clippy::neg_multiply)] #![allow(dead_code)] // use itertools::Itertools; use std::cmp::Reverse; use std::collections::{BTreeMap, BTreeSet, BinaryHeap, VecDeque}; // use superslice::Ext; // use bitset_fixed::BitSet; use proconio::{ input, marker::{Chars, Usize1}, }; #[derive(Default)] struct Solver {} impl Solver { // #[fastout] fn solve(&mut self) { input! { N: usize, M: usize, X: usize, UVCT: [(Usize1, Usize1, usize, usize); M] } let mut G = vec![vec![]; N]; for &(u, v, c, t) in UVCT.iter() { G[u].push((v, c, t)); G[v].push((u, c, t)); } let INF = 1_usize << 60; let mut dist = vec![INF; N]; dist[0] = 0; let mut Q = BinaryHeap::new(); Q.push((Reverse(0), 0, 0)); while let Some((Reverse(d), pos, money)) = Q.pop() { if dist[pos] != d { continue; } for &(nxt, c, t) in &G[pos] { let tt = if c <= money { 0 } else { (c - money + X - 1) / X }; let dd = t + tt; let next_money = tt * X + money - c; if dist[pos] + dd < dist[nxt] { dist[nxt] = dist[pos] + dd; Q.push((Reverse(dist[nxt]), nxt, next_money)); } } } let ans = dist[N - 1]; if ans == INF { println!("-1"); } else { println!("{}", ans); } } } fn main() { std::thread::Builder::new() .stack_size(128 * 1024 * 1024) .spawn(|| Solver::default().solve()) .unwrap() .join() .unwrap(); }