結果

問題 No.807 umg tours
ユーザー akakimidoriakakimidori
提出日時 2019-11-06 17:32:45
言語 Rust
(1.77.0)
結果
AC  
実行時間 242 ms / 4,000 ms
コード長 1,722 bytes
コンパイル時間 13,021 ms
コンパイル使用メモリ 380,728 KB
実行使用メモリ 44,008 KB
最終ジャッジ日時 2024-05-03 00:05:26
合計ジャッジ時間 15,152 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,812 KB
testcase_03 AC 1 ms
6,816 KB
testcase_04 AC 1 ms
6,816 KB
testcase_05 AC 1 ms
6,816 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 128 ms
33,408 KB
testcase_12 AC 118 ms
26,492 KB
testcase_13 AC 167 ms
35,648 KB
testcase_14 AC 62 ms
16,768 KB
testcase_15 AC 43 ms
13,952 KB
testcase_16 AC 187 ms
37,248 KB
testcase_17 AC 242 ms
43,692 KB
testcase_18 AC 227 ms
43,436 KB
testcase_19 AC 223 ms
43,244 KB
testcase_20 AC 115 ms
27,464 KB
testcase_21 AC 132 ms
28,408 KB
testcase_22 AC 44 ms
13,312 KB
testcase_23 AC 30 ms
10,880 KB
testcase_24 AC 76 ms
34,700 KB
testcase_25 AC 225 ms
44,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

use std::cmp::*;

#[derive(Eq, PartialEq)]
struct State {
    v: usize,
    d: u64,
}

impl Ord for State {
    fn cmp(&self, rhs: &Self) -> Ordering {
        rhs.d.cmp(&self.d)
    }
}

impl PartialOrd for State {
    fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
        Some(self.cmp(rhs))
    }
}

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 = std::collections::BinaryHeap::new();
    h.push(State{v: 0, d: 0});
    while let Some(s) = h.pop() {
        if s.d > dp[s.v] {
            continue;
        }
        for &(u, w) in &g[s.v] {
            let d = s.d + w;
            if dp[u] > d {
                dp[u] = d;
                h.push(State{v: u, d: d});
            }
        }
    }
    for i in 0..n {
        let d = dp[i] + dp[i + n];
        writeln!(out, "{}", d).ok();
    }
}

fn main() {
    run();
}
0