結果

問題 No.1473 おでぶなおばけさん
ユーザー akakimidoriakakimidori
提出日時 2021-04-09 21:32:13
言語 Rust
(1.77.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 3,996 bytes
コンパイル時間 1,991 ms
コンパイル使用メモリ 150,544 KB
実行使用メモリ 16,636 KB
最終ジャッジ日時 2023-09-07 10:02:47
合計ジャッジ時間 5,776 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 48 ms
15,872 KB
testcase_03 AC 34 ms
11,660 KB
testcase_04 AC 26 ms
9,600 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 35 ms
11,652 KB
testcase_07 AC 45 ms
16,636 KB
testcase_08 AC 50 ms
16,620 KB
testcase_09 AC 48 ms
16,620 KB
testcase_10 AC 22 ms
7,332 KB
testcase_11 AC 22 ms
7,344 KB
testcase_12 AC 22 ms
7,292 KB
testcase_13 AC 14 ms
5,232 KB
testcase_14 AC 10 ms
4,708 KB
testcase_15 AC 20 ms
6,996 KB
testcase_16 AC 20 ms
7,296 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 18 ms
6,916 KB
testcase_20 AC 28 ms
10,696 KB
testcase_21 AC 25 ms
8,444 KB
testcase_22 AC 30 ms
12,356 KB
testcase_23 AC 28 ms
10,572 KB
testcase_24 AC 25 ms
11,148 KB
testcase_25 AC 32 ms
12,000 KB
testcase_26 AC 34 ms
14,204 KB
testcase_27 AC 12 ms
5,724 KB
testcase_28 AC 46 ms
16,368 KB
testcase_29 AC 26 ms
8,776 KB
testcase_30 AC 31 ms
11,660 KB
testcase_31 AC 37 ms
14,048 KB
testcase_32 AC 26 ms
11,700 KB
testcase_33 AC 24 ms
10,364 KB
testcase_34 AC 13 ms
6,356 KB
testcase_35 AC 12 ms
5,904 KB
testcase_36 AC 23 ms
9,140 KB
testcase_37 AC 27 ms
11,332 KB
testcase_38 AC 6 ms
4,376 KB
testcase_39 AC 22 ms
7,240 KB
testcase_40 AC 22 ms
7,256 KB
testcase_41 AC 20 ms
7,340 KB
testcase_42 AC 20 ms
7,252 KB
testcase_43 AC 26 ms
11,228 KB
testcase_44 AC 26 ms
11,148 KB
testcase_45 AC 26 ms
11,184 KB
testcase_46 AC 31 ms
10,428 KB
testcase_47 AC 38 ms
11,856 KB
testcase_48 AC 35 ms
11,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//---------- begin union_find ----------
pub struct DSU {
    p: Vec<i32>,
}
impl DSU {
    pub fn new(n: usize) -> DSU {
        assert!(n < std::i32::MAX as usize);
        DSU { p: vec![-1; n] }
    }
    pub fn init(&mut self) {
        self.p.iter_mut().for_each(|p| *p = -1);
    }
    pub fn root(&self, mut x: usize) -> usize {
        assert!(x < self.p.len());
        while self.p[x] >= 0 {
            x = self.p[x] as usize;
        }
        x
    }
    pub fn same(&self, x: usize, y: usize) -> bool {
        assert!(x < self.p.len() && y < self.p.len());
        self.root(x) == self.root(y)
    }
    pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
        assert!(x < self.p.len() && y < self.p.len());
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y {
            return None;
        }
        if self.p[x] > self.p[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.p[x] += self.p[y];
        self.p[y] = x as i32;
        Some((x, y))
    }
    pub fn parent(&self, x: usize) -> Option<usize> {
        assert!(x < self.p.len());
        let p = self.p[x];
        if p >= 0 {
            Some(p as usize)
        } else {
            None
        }
    }
    pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
    where
        F: FnMut(usize),
    {
        while let Some(p) = self.parent(x) {
            f(x);
            x = p;
        }
        x
    }
    pub fn size(&self, x: usize) -> usize {
        assert!(x < self.p.len());
        let r = self.root(x);
        (-self.p[r]) as usize
    }
}
//---------- end union_find ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
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_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_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 ----------

fn run() {
    input! {
        n: usize,
        m: usize,
        e: [(usize1, usize1, u32); m],
    }
    let mut e = e;
    e.sort_by_key(|e| e.2);
    let mut dsu = DSU::new(n);
    let mut ans = std::u32::MAX;
    for &(a, b, c) in e.iter().rev() {
        if dsu.same(0, n - 1) {
            break;
        }
        dsu.unite(a, b);
        ans = c;
    }
    let mut g = vec![vec![]; n];
    for &(a, b, c) in e.iter() {
        if c >= ans {
            g[a].push(b);
            g[b].push(a);
        }
    }
    let mut dp = vec![n; n];
    let mut deq = std::collections::VecDeque::new();
    deq.push_back(0);
    dp[0] = 0;
    while let Some(v) = deq.pop_front() {
        let d = dp[v] + 1;
        for &u in g[v].iter() {
            if d < dp[u] {
                dp[u] = d;
                deq.push_back(u);
            }
        }
    }
    println!("{} {}", ans, dp[n - 1]);
}

fn main() {
    run();
}
0