pub mod binary_search { type T = usize; pub fn binary_search(mut ok: T, mut ng: T, f: impl Fn(T) -> bool) -> T { let cont = |ok: T, ng: T| { let l = ok.min(ng); let r = ok.max(ng); l + 1 < r }; while cont(ok, ng) { let x = (ok + ng) / 2; if f(x) { ok = x; } else { ng = x; } } ok } } pub mod graph { #[derive(Clone, Copy, Debug)] pub struct Edge { pub from: usize, pub to: usize, pub index: usize, pub weight: isize, pub width: isize, } pub struct Graph { pub n: usize, pub m: usize, pub edges: Vec>, } impl Graph { pub fn new(n: usize, m: usize) -> Graph { let edges = vec![vec![]; n]; Graph { n, m, edges } } pub fn add_edge( &mut self, from: usize, to: usize, index: usize, weight: isize, width: isize, ) { let edge = Edge { from, to, index, weight, width, }; self.edges[from].push(edge); } } } pub mod scanner { pub struct Scanner { buf: Vec, } impl Scanner { pub fn new() -> Self { Self { buf: vec![] } } pub fn new_from(source: &str) -> Self { let source = String::from(source); let buf = Self::split(source); Self { buf } } pub fn next(&mut self) -> T { loop { if let Some(x) = self.buf.pop() { return x.parse().ok().expect(""); } let mut source = String::new(); std::io::stdin().read_line(&mut source).expect(""); self.buf = Self::split(source); } } fn split(source: String) -> Vec { source .split_whitespace() .rev() .map(String::from) .collect::>() } } } use crate::{ binary_search::binary_search, graph::{Edge, Graph}, scanner::Scanner, }; use std::io::Write; fn main() { let mut scanner = Scanner::new(); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let t: usize = 1; for _ in 0..t { solve(&mut scanner, &mut out); } } fn shortest_path( graph: &Graph, from: usize, w: isize, ) -> (Vec>, Vec>) { #[inline] fn add(x: isize, y: isize) -> isize { x + y } #[inline] fn e() -> isize { 0 } use std::cmp::Reverse; let mut que = std::collections::BinaryHeap::>::new(); let mut dist = vec![None; graph.n]; let mut prev = vec![None; graph.n]; que.push(Reverse((e(), from))); dist[from] = Some(e()); while let Some(Reverse((d, u))) = que.pop() { if let Some(now_d) = dist[u] { if now_d < d { continue; } } for edge in graph.edges[u].iter() { let &Edge { to: v, weight, width, .. } = edge; if width < w { continue; } let next_d = add(d, weight); if let Some(now_d) = dist[v] { if now_d <= next_d { continue; } } que.push(Reverse((next_d, v))); dist[v] = Some(next_d); prev[v] = Some((u, *edge)); } } (dist, prev) } fn solve(scanner: &mut Scanner, out: &mut std::io::BufWriter) { let n: usize = scanner.next(); let m: usize = scanner.next(); let x: usize = scanner.next(); let mut graph = Graph::new(n, m); for index in 0..m { let from: usize = scanner.next::() - 1; let to: usize = scanner.next::() - 1; let weight: isize = scanner.next(); let width: isize = scanner.next(); graph.add_edge(from, to, index, weight, width); graph.add_edge(to, from, index, weight, width); } let ans = binary_search(0, 1e9 as usize + 10, |w| { let dist = shortest_path(&graph, 0, w as isize); dist.0[n - 1].is_some() && dist.0[n - 1].unwrap() <= x as isize }); let ans = if ans == 0 { -1 as isize } else { ans as isize }; writeln!(out, "{}", ans).unwrap(); }