結果
問題 | No.95 Alice and Graph |
ユーザー | nebocco |
提出日時 | 2021-02-12 20:28:00 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 2,639 ms / 5,000 ms |
コード長 | 5,566 bytes |
コンパイル時間 | 16,078 ms |
コンパイル使用メモリ | 378,184 KB |
実行使用メモリ | 8,640 KB |
最終ジャッジ日時 | 2024-12-05 18:34:36 |
合計ジャッジ時間 | 28,328 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 741 ms
8,604 KB |
testcase_01 | AC | 2,639 ms
8,480 KB |
testcase_02 | AC | 2,634 ms
8,640 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 387 ms
5,248 KB |
testcase_05 | AC | 316 ms
5,400 KB |
testcase_06 | AC | 338 ms
5,272 KB |
testcase_07 | AC | 2,415 ms
8,604 KB |
testcase_08 | AC | 1 ms
5,248 KB |
testcase_09 | AC | 1 ms
5,248 KB |
testcase_10 | AC | 1 ms
5,248 KB |
testcase_11 | AC | 1 ms
5,248 KB |
testcase_12 | AC | 1 ms
5,248 KB |
testcase_13 | AC | 722 ms
5,248 KB |
ソースコード
// #[allow(unused_macros)] // macro_rules! input { // (@read $io:tt @rest) => {}; // (@read $io:tt @rest mut $($rest:tt)*) => { // input!(@read $io @mut [mut] @rest $($rest)*) // }; // (@read $io:tt @rest $($rest:tt)*) => { // input!(@read $io @mut [] @rest $($rest)*) // }; // (@read $io:tt @mut [$($mut:tt)?] @rest $var:tt: $($rest:tt)*) => { // input!(@read $io @mut[$($mut)*] @var $var @kind [] @rest $($rest)*) // }; // (@read $io:tt @mut [$($mut:tt)?] @var $var:tt @kind [$($kind:tt)*] @rest) => { // let $($mut)* $var = input!(@kind $io:tt $($kind)*); // }; // (@read $io:tt @mut [$($mut:tt)?] @var $var:tt @kind [$($kind:tt)*] @rest, $($rest:tt)*) => { // input!(@read $io @mut [$($mut)*] @var $var @kind [$($kind)*] @rest); // input!(@read $io @rest $($rest)*); // }; // (@read $io:tt @mut [$($mut:tt)?] @var $var:tt @kind [$($kind:tt)*] @rest $tt:tt $($rest:tt)*) => { // input!(@read $io @mut [$($mut)*] @var $var @kind [$($kind)* $tt] @rest $($rest)*); // }; // (@kind $io:tt [$kind:tt; $len:tt]) => { // io.scan_vec::<$kind>($len); // }; // (@kind $io:tt $kind:tt) => { // io.scan::<$kind>(); // }; // ($($rest:tt)*) => { // let mut io = IO::new(); // input!(@read io @rest $($rest)*) // }; // } fn main() { // input!{ // n: usize, _m: usize, k: i32, // ed: [(usize, usize); _m], // } let mut io = IO::new(); let (n, _m, k) = io.scan(); let ed = io.scan_vec::<(usize, usize)>(_m); let mut d = vec![vec![k+1; n]; n]; for i in 0..n { d[i][i] = 0; } for &(u, v) in &ed { d[u-1][v-1] = 1; d[v-1][u-1] = 1; } for v in 0..n { for u in 0..n { for w in 0..n { d[u][w] = std::cmp::min(d[u][w], d[u][v] + d[v][w]); } } } let mut vis = Vec::new(); for i in (1..n).rev() { vis.push(i); let m = vis.len(); let mut dp = vec![vec![k+1; m]; 1<<m]; for j in 0..m { dp[1<<j][j] = d[0][vis[j]]; } for bit in 1..1<<m { for j in 0..m { if bit & (1 << j) == 0 { continue; } if dp[bit][j] > k { continue; } for jj in 0..m { if bit & (1 << jj) == 0 { dp[bit | 1 << jj][jj] = std::cmp::min( dp[bit | 1 << jj][jj], dp[bit][j] + d[vis[j]][vis[jj]] ); } } } } let goal = (1 << m) - 1; if (0..m).all(|j| dp[goal][j] > k) { vis.pop(); } } println!("{}", vis.iter().map(|i| (1i64 << i) - 1).sum::<i64>()); } // ------------ io module start ------------ use std::io::{stdout, BufWriter, Read, StdoutLock, Write}; pub struct IO { iter: std::str::SplitAsciiWhitespace<'static>, buf: BufWriter<StdoutLock<'static>>, } impl IO { pub fn new() -> Self { let mut input = String::new(); std::io::stdin().read_to_string(&mut input).unwrap(); let input = Box::leak(input.into_boxed_str()); let out = Box::new(stdout()); IO { iter: input.split_ascii_whitespace(), buf: BufWriter::new(Box::leak(out).lock()), } } fn scan_str(&mut self) -> &'static str { self.iter.next().unwrap() } fn scan_raw(&mut self) -> &'static [u8] { self.scan_str().as_bytes() } pub fn scan<T: Scan>(&mut self) -> T { T::scan(self) } pub fn scan_vec<T: Scan>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.scan()).collect() } } impl IO { pub fn print<T: Print>(&mut self, x: T) { T::print(self, x); } pub fn println<T: Print>(&mut self, x: T) { self.print(x); self.print("\n"); } pub fn iterln<T: Print, I: Iterator<Item = T>>(&mut self, mut iter: I, delim: &str) { if let Some(v) = iter.next() { self.print(v); for v in iter { self.print(delim); self.print(v); } } self.print("\n"); } pub fn flush(&mut self) { self.buf.flush().unwrap(); } } impl Default for IO { fn default() -> Self { Self::new() } } pub trait Scan { fn scan(io: &mut IO) -> Self; } macro_rules! impl_parse_int { ($($t:tt),*) => { $( impl Scan for $t { fn scan(s: &mut IO) -> Self { let mut res = 0; let mut neg = false; for d in s.scan_raw() { if *d == b'-' { neg = true; } else { res *= 10; res += (*d - b'0') as $t; } } if neg { res = res.wrapping_neg(); } res } } )* }; } impl_parse_int!(i16, i32, i64, isize, u16, u32, u64, usize); impl<T: Scan, U: Scan> Scan for (T, U) { fn scan(s: &mut IO) -> Self { (T::scan(s), U::scan(s)) } } impl<T: Scan, U: Scan, V: Scan> Scan for (T, U, V) { fn scan(s: &mut IO) -> Self { (T::scan(s), U::scan(s), V::scan(s)) } } impl<T: Scan, U: Scan, V: Scan, W: Scan> Scan for (T, U, V, W) { fn scan(s: &mut IO) -> Self { (T::scan(s), U::scan(s), V::scan(s), W::scan(s)) } } pub trait Print { fn print(w: &mut IO, x: Self); } macro_rules! impl_print_int { ($($t:ty),*) => { $( impl Print for $t { fn print(w: &mut IO, x: Self) { w.buf.write_all(x.to_string().as_bytes()).unwrap(); } } )* }; } impl_print_int!(i16, i32, i64, isize, u16, u32, u64, usize); impl Print for u8 { fn print(w: &mut IO, x: Self) { w.buf.write_all(&[x]).unwrap(); } } impl Print for &[u8] { fn print(w: &mut IO, x: Self) { w.buf.write_all(x).unwrap(); } } impl Print for &str { fn print(w: &mut IO, x: Self) { w.print(x.as_bytes()); } } impl<T: Print, U: Print> Print for (T, U) { fn print(w: &mut IO, (x, y): Self) { w.print(x); w.print(" "); w.print(y); } } impl<T: Print, U: Print, V: Print> Print for (T, U, V) { fn print(w: &mut IO, (x, y, z): Self) { w.print(x); w.print(" "); w.print(y); w.print(" "); w.print(z); } } // ------------ io module end ------------