結果

問題 No.95 Alice and Graph
ユーザー akakimidoriakakimidori
提出日時 2022-11-15 05:04:16
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,607 ms / 5,000 ms
コード長 3,623 bytes
コンパイル時間 2,946 ms
コンパイル使用メモリ 151,160 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 10:57:25
合計ジャッジ時間 8,388 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 610 ms
4,348 KB
testcase_01 AC 88 ms
4,352 KB
testcase_02 AC 71 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 804 ms
4,352 KB
testcase_05 AC 648 ms
4,348 KB
testcase_06 AC 775 ms
4,348 KB
testcase_07 AC 87 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1,607 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    input! {
        n: usize,
        m: usize,
        k: i8,
        e: [(usize1, usize1); m],
    }
    let inf = std::i8::MAX / 2;
    let mut g = vec![vec![k + 1; 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![k + 1; 1 << n]; n];
        let mut x = 0;
        let mut pos = vec![];
        for i in 0..60 {
            if bit >> i & 1 == 1 {
                dp[x][1 << x] = g[0][i];
                pos.push(i);
                x += 1;
            }
        }
        for bit in 1..(1 << n) {
            for last in 0..n {
                if dp[last][bit] == inf {
                    continue;
                }
                for next in 0..n {
                    if bit >> next & 1 == 0 {
                        let d = dp[last][bit] + g[pos[last]][pos[next]];
                        dp[next][bit | (1 << next)].chmin(d);
                    }
                }
            }
        }
        dp.iter().any(|dp| dp[(1 << n) - 1] <= 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);
}

// ---------- 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