use std::{borrow::Cow, cmp::Reverse, collections::BinaryHeap}; use proconio::{input, marker::Usize1}; fn main() { input! { (num_islands, num_edges, max_c): (usize, usize, usize), ijrm: [(Usize1, Usize1, usize, usize); num_edges], } let mut graph = vec![vec![]; num_islands]; for &(i, j, r, m) in &ijrm { graph[i].push(Edge { from: i, to: j, denom: r, base_cost: m, }); graph[j].push(Edge { from: j, to: i, denom: r, base_cost: m, }); } let solve = |init_money: usize| { let mut sum_costs: Vec> = vec![None; num_islands]; let mut heap = BinaryHeap::from([(Reverse(0_usize), 0_usize)]); while let Some((Reverse(sum_cost), cur)) = heap.pop() { if sum_costs[cur].is_some() { continue; } sum_costs[cur] = Some(sum_cost); let next_node_iter = graph[cur].iter().filter_map(|edge| { let next_cost = sum_cost + (init_money - sum_cost) / edge.denom + edge.base_cost; if next_cost <= init_money { Some((Reverse(next_cost), edge.to)) } else { None } }); heap.extend(next_node_iter); } sum_costs[num_islands - 1].map(|sum_cost| init_money - sum_cost) }; for c in 1..=max_c { let ans = solve(c); println!( "{}", match ans { Some(rem) => rem.to_string().into(), None => Cow::Borrowed("-1"), } ); } } #[allow(dead_code)] #[derive(Debug, Clone, Copy)] struct Edge { from: usize, to: usize, denom: usize, base_cost: usize, }