#![allow(unused_imports)] //use itertools::{iproduct, Itertools}; use proconio::input; use proconio::marker::*; use std::collections::*; fn main() { 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, t * x + c)); g[v].push((u, t * x + c)); } let neigh = |crr_pos: usize, crr_dist: usize| { let mut nxts = vec![]; for &(nxt, cost) in g[crr_pos].iter() { nxts.push((nxt, crr_dist + cost)); } nxts }; let dists = Dijkstra::new(neigh).solve(n, 0); if dists[n - 1] == INF { println!("-1"); } else { let ans = (dists[n - 1] + x - 1) / x; println!("{}", ans); } } const INF: usize = std::usize::MAX; use dijkstra::*; mod dijkstra { use std::cmp::Reverse; pub struct Dijkstra where F: Fn(usize, usize) -> Vec<(usize, usize)>, //(crr_pos,crr_dist)->Vec<(nxt_pos,nxt_dist)> { neigh: F, } impl Dijkstra where F: Fn(usize, usize) -> Vec<(usize, usize)>, { pub fn new(neigh: F) -> Self { Self { neigh } } pub fn solve(&self, n: usize, start: usize) -> Vec { let mut dists = vec![INF; n]; dists[start] = 0; let mut pq = std::collections::BinaryHeap::new(); pq.push(Reverse((0, start))); while let Some(Reverse((crr_dist, crr_pos))) = pq.pop() { if dists[crr_pos] < crr_dist { continue; } for (nxt_pos, nxt_dist) in (self.neigh)(crr_pos, crr_dist) { if nxt_dist < dists[nxt_pos] { dists[nxt_pos] = nxt_dist; pq.push(Reverse((nxt_dist, nxt_pos))); } } } dists } } const INF: usize = std::usize::MAX; }