結果

問題 No.95 Alice and Graph
ユーザー akakimidoriakakimidori
提出日時 2022-11-15 05:17:12
言語 Rust
(1.77.0)
結果
AC  
実行時間 160 ms / 5,000 ms
コード長 4,009 bytes
コンパイル時間 1,328 ms
コンパイル使用メモリ 159,972 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 11:05:44
合計ジャッジ時間 3,062 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
4,352 KB
testcase_01 AC 21 ms
4,348 KB
testcase_02 AC 12 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 145 ms
4,352 KB
testcase_05 AC 129 ms
4,348 KB
testcase_06 AC 138 ms
4,352 KB
testcase_07 AC 20 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,356 KB
testcase_13 AC 160 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    input! {
        n: usize,
        m: usize,
        k: i8,
        e: [(usize1, usize1); m],
    }
    let inf = k + 1;
    let mut g = vec![vec![inf; n]; n];
    for i in 0..n {
        g[i][i] = 0;
    }
    for (a, b) in e {
        g[a][b] = 1;
        g[b][a] = 1;
    }
    for k in 0..n {
        for i in 0..n {
            for j in 0..n {
                g[i][j] = g[i][j].min(g[i][k] + g[k][j]);
            }
        }
    }
    let can = |bit: usize| -> bool {
        if bit.count_ones() > (k as u32) {
            return false;
        }
        let n = bit.count_ones() as usize;
        let mut dp = vec![vec![inf; 1 << n]; n];
        let mut x = 0;
        let mut pos = vec![];
        for i in 0..60 {
            if bit >> i & 1 == 1 {
                dp[x][((1 << n) - 1) ^ (1 << x)] = g[0][i];
                pos.push(i);
                x += 1;
            }
        }
        let g = pos.iter().map(|x| pos.iter().map(|y| g[*x][*y]).collect()).collect::<Vec<Vec<_>>>();
        for bit in (1..(1 << n)).rev() {
            for last in One(!bit & ((1 << n) - 1)) {
                let d = dp[last][bit];
                if d == inf {
                    continue;
                }
                let g = &g[last];
                for next in One(bit) {
                    let d = d + g[next];
                    dp[next][bit ^ (1 << next)].chmin(d);
                }
            }
        }
        dp.iter().any(|dp| dp[0] <= k)
    };
    let mut bit = 0;
    for i in (1..n).rev() {
        if can(bit | (1 << i)) {
            bit |= 1 << i;
        }
    }
    let mut ans = 0u64;
    for i in 0..n {
        if bit >> i & 1 == 1 {
            ans += (1 << i) - 1;
        }
    }
    println!("{}", ans);
}

pub struct One(usize);

impl Iterator for One {
    type Item = usize;
    fn next(&mut self) -> Option<Self::Item> {
        if self.0 == 0 {
            None
        } else {
            let k = self.0.trailing_zeros() as usize;
            self.0 ^= 1 << k;
            Some(k)
        }
    }
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------
// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
0