結果
問題 | No.2565 はじめてのおつかい |
ユーザー |
![]() |
提出日時 | 2023-12-09 19:42:49 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 42 ms / 2,000 ms |
コード長 | 2,636 bytes |
コンパイル時間 | 13,918 ms |
コンパイル使用メモリ | 379,512 KB |
実行使用メモリ | 11,520 KB |
最終ジャッジ日時 | 2024-09-27 03:44:44 |
合計ジャッジ時間 | 16,747 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 42 ms
11,520 KB |
testcase_04 | AC | 23 ms
11,392 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 15 ms
5,376 KB |
testcase_07 | AC | 8 ms
5,376 KB |
testcase_08 | AC | 9 ms
5,376 KB |
testcase_09 | AC | 13 ms
5,376 KB |
testcase_10 | AC | 10 ms
5,376 KB |
testcase_11 | AC | 26 ms
7,168 KB |
testcase_12 | AC | 5 ms
5,376 KB |
testcase_13 | AC | 10 ms
5,376 KB |
testcase_14 | AC | 17 ms
5,376 KB |
testcase_15 | AC | 18 ms
5,376 KB |
testcase_16 | AC | 20 ms
8,704 KB |
testcase_17 | AC | 4 ms
5,376 KB |
testcase_18 | AC | 6 ms
5,376 KB |
testcase_19 | AC | 21 ms
6,400 KB |
testcase_20 | AC | 19 ms
6,016 KB |
testcase_21 | AC | 19 ms
6,144 KB |
testcase_22 | AC | 10 ms
5,376 KB |
testcase_23 | AC | 12 ms
5,376 KB |
testcase_24 | AC | 3 ms
5,376 KB |
testcase_25 | AC | 21 ms
6,144 KB |
testcase_26 | AC | 12 ms
5,376 KB |
testcase_27 | AC | 19 ms
6,400 KB |
testcase_28 | AC | 22 ms
6,144 KB |
testcase_29 | AC | 26 ms
7,552 KB |
testcase_30 | AC | 21 ms
7,168 KB |
testcase_31 | AC | 19 ms
6,272 KB |
testcase_32 | AC | 12 ms
5,376 KB |
testcase_33 | AC | 20 ms
6,656 KB |
testcase_34 | AC | 19 ms
5,632 KB |
testcase_35 | AC | 23 ms
7,808 KB |
testcase_36 | AC | 21 ms
6,528 KB |
testcase_37 | AC | 12 ms
5,376 KB |
testcase_38 | AC | 19 ms
5,632 KB |
testcase_39 | AC | 16 ms
5,376 KB |
testcase_40 | AC | 23 ms
7,552 KB |
testcase_41 | AC | 25 ms
8,064 KB |
testcase_42 | AC | 11 ms
5,376 KB |
testcase_43 | AC | 17 ms
5,504 KB |
testcase_44 | AC | 9 ms
5,376 KB |
testcase_45 | AC | 5 ms
5,376 KB |
testcase_46 | AC | 18 ms
5,376 KB |
testcase_47 | AC | 11 ms
5,376 KB |
testcase_48 | AC | 10 ms
5,376 KB |
testcase_49 | AC | 5 ms
5,376 KB |
testcase_50 | AC | 12 ms
5,376 KB |
testcase_51 | AC | 1 ms
5,376 KB |
testcase_52 | AC | 8 ms
5,376 KB |
ソースコード
#![allow(non_snake_case, unused_imports, unused_must_use)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; ($T: ty, $N: expr) => { (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>() }; } let N = input!(usize); let M = input!(usize); let mut graph = vec![vec![]; N]; for _ in 0..M { let (u, v) = (input!(usize) - 1, input!(usize) - 1); graph[u].push(v); } let bfs = |start: usize| { let mut dist = vec![1_usize << 30; N]; let mut seen = vec![false; N]; let mut q = std::collections::VecDeque::new(); dist[start] = 0; seen[start] = true; q.push_front(start); while let Some(now) = q.pop_front() { for &nxt in graph[now].iter() { if seen[nxt] { continue; } dist[nxt] = dist[now] + 1; seen[nxt] = true; q.push_back(nxt); } } return dist; }; let dist_from0 = bfs(0); let dist_fromNm1 = bfs(N - 2); let dist_fromN = bfs(N - 1); let mut ans = 1_usize << 30; // 1 -> N - 1 -> N -> 1 let ans1 = dist_from0[N - 2] + dist_fromNm1[N - 1] + dist_fromN[0]; // 1 -> N -> N - 1 -> 1 let ans2 = dist_from0[N - 1] + dist_fromN[N - 2] + dist_fromNm1[0]; ans = std::cmp::min(ans, ans1); ans = std::cmp::min(ans, ans2); if ans == 1 << 30 { writeln!(out, "-1"); } else { writeln!(out, "{}", ans); } } struct Scanner<R> { reader: R, buf_str: Vec<u8>, buf_iter: str::SplitWhitespace<'static>, } impl<R: BufRead> Scanner<R> { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token<T: str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }