結果
問題 | No.1 道のショートカット |
ユーザー |
|
提出日時 | 2024-06-17 22:13:50 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 1,872 bytes |
コンパイル時間 | 13,552 ms |
コンパイル使用メモリ | 377,724 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-17 22:14:07 |
合計ジャッジ時間 | 15,809 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
use std::collections::VecDeque;#[allow(unused_macros)]macro_rules! read {([$t:ty] ; $n:expr) =>((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());($($t:ty),+ ; $n:expr) =>((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());([$t:ty]) =>(rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());($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<usize> = read!([usize]);let tt: Vec<usize> = read!([usize]);let yy: Vec<usize> = read!([usize]);let mm: Vec<u32> = read!([u32]);let g: Vec<Vec<(usize, usize, u32)>> = {let mut graph: Vec<Vec<(usize, usize, u32)>> = 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<u32>> = 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);}}