結果

問題 No.807 umg tours
ユーザー akakimidoriakakimidori
提出日時 2019-11-06 17:53:06
言語 Rust
(1.77.0)
結果
AC  
実行時間 464 ms / 4,000 ms
コード長 4,289 bytes
コンパイル時間 11,230 ms
コンパイル使用メモリ 397,656 KB
実行使用メモリ 50,276 KB
最終ジャッジ日時 2024-05-03 00:05:47
合計ジャッジ時間 16,385 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 0 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 0 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 173 ms
33,408 KB
testcase_12 AC 198 ms
29,056 KB
testcase_13 AC 292 ms
37,760 KB
testcase_14 AC 101 ms
18,432 KB
testcase_15 AC 69 ms
14,464 KB
testcase_16 AC 298 ms
38,420 KB
testcase_17 AC 406 ms
47,440 KB
testcase_18 AC 401 ms
46,688 KB
testcase_19 AC 362 ms
45,168 KB
testcase_20 AC 188 ms
27,804 KB
testcase_21 AC 186 ms
28,928 KB
testcase_22 AC 73 ms
13,696 KB
testcase_23 AC 49 ms
11,008 KB
testcase_24 AC 79 ms
40,064 KB
testcase_25 AC 464 ms
50,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin pairing heap (min) ----------
#[allow(dead_code)]
mod pairing {
    use std;
    // min heap
    pub struct Heap<T: Ord> {
        root: Link<T>,
        size: usize,
    }
    impl<T: Ord> Heap<T> {
        pub fn new() -> Heap<T> {
            Heap {
                root: None,
                size: 0,
            }
        }
        pub fn push(&mut self, val: T) {
            let a = self.root.take();
            let b = Some(Node::new_node(val));
            self.root = Node::meld(a, b);
            self.size += 1;
        }
        pub fn pop(&mut self) -> Option<T> {
            self.root.take().map(|mut root| {
                self.size -= 1;
                let lst = root.child.take();
                self.root = Node::merge_list(lst);
                root.val
            })
        }
        pub fn peek(&self) -> Option<&T> {
            self.root.as_ref().map(|root| &root.val)
        }
        pub fn len(&self) -> usize {
            self.size
        }
        pub fn clear(&mut self) {
            self.root = None;
            self.size = 0;
        }
        pub fn is_empty(&self) -> bool {
            self.len() > 0
        }
    }
    type NonNull<T> = Box<Node<T>>;
    type Link<T> = Option<NonNull<T>>;
    struct Node<T> {
        val: T,
        next: Link<T>,
        child: Link<T>,
    }
    impl<T: Ord> Node<T> {
        fn new_node(val: T) -> NonNull<T> {
            Box::new(Node {
                val: val,
                next: None,
                child: None,
            })
        }
        fn meld(a: Link<T>, b: Link<T>) -> Link<T> {
            if a.is_none() {
                return b;
            }
            if b.is_none() {
                return a;
            }
            let mut a = a.unwrap();
            let mut b = b.unwrap();
            match a.val.cmp(&b.val) {
                std::cmp::Ordering::Less => {
                    b.next = a.child.take();
                    a.child = Some(b);
                    Some(a)
                },
                _ => {
                    a.next = b.child.take();
                    b.child = Some(a);
                    Some(b)
                },
            }
        }
        fn merge_list(mut lst: Link<T>) -> Link<T> {
            let mut r = None;
            while !lst.is_none() {
                let mut a = lst.take().unwrap();
                lst = a.next.take();
                if lst.is_none() {
                    lst = Some(a);
                    break;
                } else {
                    let mut b = lst.take().unwrap();
                    lst = b.next.take();
                    r = Node::meld(r, Node::meld(Some(a), Some(b)));
                }
            }
            if !lst.is_none() {
                r = Node::meld(r, lst);
            }
            r
        }
    }
}
// ---------- end pairing heap (min) ----------

use std::io::Read;
use std::io::Write;

fn run() {
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let m: usize = it.next().unwrap().parse().unwrap();
    let mut g = vec![vec![]; 2 * n];
    for _ in 0..m {
        let a = it.next().unwrap().parse::<usize>().unwrap() - 1;
        let b = it.next().unwrap().parse::<usize>().unwrap() - 1;
        let c = it.next().unwrap().parse::<u64>().unwrap();
        g[a].push((b, c));
        g[b].push((a, c));
        g[a].push((b + n, 0));
        g[b].push((a + n, 0));
        g[a + n].push((b + n, c));
        g[b + n].push((a + n, c));
    }
    let inf = std::u64::MAX;
    let mut dp = vec![inf; 2 * n];
    dp[0] = 0;
    dp[n] = 0;
    let mut h = pairing::Heap::<(u64, usize)>::new();
    h.push((0, 0));
    while let Some(s) = h.pop() {
        if s.0 > dp[s.1] {
            continue;
        }
        for &(u, w) in &g[s.1] {
            let d = s.0 + w;
            if dp[u] > d {
                dp[u] = d;
                h.push((d, u));
            }
        }
    }
    for i in 0..n {
        let d = dp[i] + dp[i + n];
        writeln!(out, "{}", d).ok();
    }
}

fn main() {
    run();
}
0