use std::collections::VecDeque; #[allow(unused_macros)] macro_rules! read { ([$t:ty] ; $n:expr) => ((0..$n).map(|_| read!([$t])).collect::>()); ($($t:ty),+ ; $n:expr) => ((0..$n).map(|_| read!($($t),+)).collect::>()); ([$t:ty]) => (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::>()); ($t:ty) => (rl().parse::<$t>().unwrap()); ($($t:ty),*) => {{ let buf = rl(); let mut w = buf.split_whitespace(); ($(w.next().unwrap().parse::<$t>().unwrap()),*) }}; } #[allow(dead_code)] fn rl() -> String { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().to_owned() } fn main(){ let n: usize = read!(usize); let c: usize = read!(usize); let _v: usize = read!(usize); let ss: Vec = read!([usize]); let tt: Vec = read!([usize]); let yy: Vec = read!([usize]); let mm: Vec = read!([u32]); let g: Vec> = { let mut graph: Vec> = vec![vec![]; n]; for (((s, t), y), m) in ss.into_iter().zip(tt).zip(yy).zip(mm){ graph[s - 1].push((t - 1, y, m)); } graph }; let mut cost: Vec> = vec![vec![u32::max_value(); n]; c + 1]; cost[c][0] = 0; let mut que: VecDeque<(usize, usize)> = VecDeque::from(vec![(c, 0)]); while let Some((c, s)) = que.pop_front(){ for &(t, y, m) in &g[s]{ if y <= c && cost[c][s] + m < cost[c - y][t]{ cost[c - y][t] = cost[c][s] + m; que.push_back((c - y, t)); } } } let ans: u32 = (0..=c).map(|c: usize| cost[c][n - 1]).min().unwrap(); if ans == u32::max_value() { println!("-1"); } else{ println!("{}", ans); } }