結果
問題 | No.2565 はじめてのおつかい |
ユーザー | ikd |
提出日時 | 2023-12-02 16:03:03 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,939 bytes |
コンパイル時間 | 13,784 ms |
コンパイル使用メモリ | 377,976 KB |
実行使用メモリ | 11,256 KB |
最終ジャッジ日時 | 2024-09-26 19:45:23 |
合計ジャッジ時間 | 16,389 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 34 ms
11,216 KB |
testcase_04 | AC | 23 ms
11,256 KB |
testcase_05 | WA | - |
testcase_06 | AC | 17 ms
5,376 KB |
testcase_07 | AC | 8 ms
5,376 KB |
testcase_08 | AC | 9 ms
5,376 KB |
testcase_09 | AC | 16 ms
5,376 KB |
testcase_10 | AC | 12 ms
5,376 KB |
testcase_11 | AC | 24 ms
7,072 KB |
testcase_12 | AC | 7 ms
5,376 KB |
testcase_13 | AC | 10 ms
5,376 KB |
testcase_14 | AC | 17 ms
5,632 KB |
testcase_15 | AC | 18 ms
5,376 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 23 ms
7,556 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | AC | 19 ms
5,888 KB |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | AC | 16 ms
5,376 KB |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | AC | 12 ms
5,376 KB |
testcase_43 | WA | - |
testcase_44 | AC | 9 ms
5,376 KB |
testcase_45 | AC | 5 ms
5,376 KB |
testcase_46 | AC | 19 ms
5,504 KB |
testcase_47 | AC | 12 ms
5,376 KB |
testcase_48 | AC | 10 ms
5,376 KB |
testcase_49 | AC | 6 ms
5,376 KB |
testcase_50 | AC | 14 ms
5,376 KB |
testcase_51 | AC | 0 ms
5,376 KB |
testcase_52 | AC | 12 ms
5,376 KB |
ソースコード
use std::{io, collections::VecDeque}; use scanner::Scanner; const INF: u64 = u64::MAX / 2; fn bfs(s: usize, g: &Vec<Vec<usize>>) -> Vec<u64> { let mut dist = vec![INF; g.len()]; let mut que = VecDeque::new(); dist[s] = 0; que.push_back(s); while let Some(v) = que.pop_front() { for &x in &g[v] { if dist[x] == INF { dist[x] = dist[v] + 1; que.push_back(x); } } } dist } fn main() { let mut scanner = Scanner::from(io::stdin().lock()); let (n, m) = scan!((usize, usize), <~ scanner); let edges = scan!([(usize, usize); m], <~ scanner); let mut g = vec![vec![]; n]; for (u, v) in edges { g[u - 1].push(v - 1); } let (x, y) = (n - 2, n - 1); let d_0 = bfs(0, &g); let d_x = bfs(x, &g); let d_y = bfs(y, &g); let mut ans = INF; // 0 -> x -> y -> 0 ans = ans.min(d_0[x] + d_x[y] + d_y[0]); // 0 -> y -> x -> 0 ans = ans.min(d_0[y] + d_y[x] + d_x[0]); if ans == INF { println!("-1"); } else { println!("{}", ans); } } // ✂ --- scanner --- ✂ #[allow(unused)] mod scanner { use std::fmt; use std::io; use std::str; pub struct Scanner<R> { r: R, l: String, i: usize, } impl<R> Scanner<R> where R: io::BufRead, { pub fn new(reader: R) -> Self { Self { r: reader, l: String::new(), i: 0, } } pub fn scan<T>(&mut self) -> T where T: str::FromStr, T::Err: fmt::Debug, { self.skip_blanks(); assert!(self.i < self.l.len()); // remain some character assert_ne!(&self.l[self.i..=self.i], " "); let rest = &self.l[self.i..]; let len = rest .find(|ch| char::is_ascii_whitespace(&ch)) .unwrap_or_else(|| rest.len()); // parse self.l[self.i..(self.i + len)] let val = rest[..len] .parse() .unwrap_or_else(|e| panic!("{:?}, attempt to read `{}`", e, rest)); self.i += len; val } pub fn scan_vec<T>(&mut self, n: usize) -> Vec<T> where T: str::FromStr, T::Err: fmt::Debug, { (0..n).map(|_| self.scan()).collect::<Vec<_>>() } fn skip_blanks(&mut self) { loop { match self.l[self.i..].find(|ch| !char::is_ascii_whitespace(&ch)) { Some(j) => { self.i += j; break; } None => { self.l.clear(); // clear buffer let num_bytes = self .r .read_line(&mut self.l) .unwrap_or_else(|_| panic!("invalid UTF-8")); assert!(num_bytes > 0, "reached EOF :("); self.i = 0; } } } } } impl<'a> From<&'a str> for Scanner<&'a [u8]> { fn from(s: &'a str) -> Self { Self::new(s.as_bytes()) } } impl<'a> From<io::StdinLock<'a>> for Scanner<io::BufReader<io::StdinLock<'a>>> { fn from(stdin: io::StdinLock<'a>) -> Self { Self::new(io::BufReader::new(stdin)) } } #[macro_export] macro_rules! scan { (( $($t: ty),+ ), <~ $scanner: expr) => { ( $(scan!($t, <~ $scanner)),+ ) }; ([ $t: tt; $n: expr ], <~ $scanner: expr) => { (0..$n).map(|_| scan!($t, <~ $scanner)).collect::<Vec<_>>() }; ($t: ty, <~ $scanner: expr) => { $scanner.scan::<$t>() }; } } // ✂ --- scanner --- ✂