結果

問題 No.1308 ジャンプビーコン
ユーザー koba-e964koba-e964
提出日時 2023-08-09 15:05:03
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 3,164 ms / 4,000 ms
コード長 5,822 bytes
コンパイル時間 15,155 ms
コンパイル使用メモリ 379,024 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-15 11:01:24
合計ジャッジ時間 73,178 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 3 ms
5,248 KB
testcase_09 AC 3 ms
5,248 KB
testcase_10 AC 3 ms
5,248 KB
testcase_11 AC 3 ms
5,248 KB
testcase_12 AC 3 ms
5,248 KB
testcase_13 AC 155 ms
5,248 KB
testcase_14 AC 153 ms
5,248 KB
testcase_15 AC 144 ms
5,248 KB
testcase_16 AC 145 ms
5,248 KB
testcase_17 AC 145 ms
5,248 KB
testcase_18 AC 3,160 ms
5,248 KB
testcase_19 AC 3,164 ms
5,248 KB
testcase_20 AC 3,019 ms
5,248 KB
testcase_21 AC 2,968 ms
5,248 KB
testcase_22 AC 2,922 ms
5,248 KB
testcase_23 AC 1 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 2,880 ms
5,248 KB
testcase_27 AC 2,970 ms
5,248 KB
testcase_28 AC 2,783 ms
5,248 KB
testcase_29 AC 2,778 ms
5,248 KB
testcase_30 AC 2,716 ms
5,248 KB
testcase_31 AC 2,625 ms
5,248 KB
testcase_32 AC 2,821 ms
5,248 KB
testcase_33 AC 2,863 ms
5,248 KB
testcase_34 AC 2,858 ms
5,248 KB
testcase_35 AC 2,861 ms
5,248 KB
testcase_36 AC 2,894 ms
5,248 KB
testcase_37 AC 2,884 ms
5,248 KB
testcase_38 AC 3,156 ms
5,248 KB
testcase_39 AC 3,128 ms
5,248 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `BufWriter`
 --> src/main.rs:5:22
  |
5 | use std::io::{Write, BufWriter};
  |                      ^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `Write`
 --> src/main.rs:5:15
  |
5 | use std::io::{Write, BufWriter};
  |               ^^^^^

warning: unused variable: `lca`
   --> src/main.rs:155:9
    |
155 |     let lca = LCA::new(&mut ch, &pars, 0);
    |         ^^^ help: if this is intentional, prefix it with an underscore: `_lca`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: field `dep` is never read
  --> src/main.rs:50:5
   |
46 | pub struct LCA {
   |            --- field in this struct
...
50 |     dep: Vec<usize>,
   |     ^^^
   |
   = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

pub struct LCA {
    st: Vec<usize>,
    par: Vec<usize>,
    jmp: Vec<usize>,
    dep: Vec<usize>,
}

// Constant-factor speedup used in https://codeforces.com/contest/1083/submission/46874242.
// Base on HL-decomposition.
// par[root] = root should hold.
// These functions use O(n) stack space.
// Verified by https://codeforces.com/contest/1083/submission/51934575.
impl LCA {
    // For each node, make the most heavy child the first child.
    fn dfs_left(ch: &mut [Vec<usize>], v: usize, sz: &mut [usize],
                dep: &mut [usize], d: usize) {
        dep[v] = d;
        let mut s = 1;
        for i in 0..ch[v].len() {
            let w = ch[v][i];
            Self::dfs_left(ch, w, sz, dep, d + 1);
            s += sz[w];
            if sz[w] > sz[ch[v][0]] {
                ch[v].swap(i, 0);
            }
        }
        sz[v] = s;
    }
    fn dfs(ch: &[Vec<usize>], st: &mut [usize], v: usize,
           cnt: &mut usize, jmp: &mut [usize]) {
        st[v] = *cnt;
        *cnt += 1;
        if ch[v].len() >= 1 {
            jmp[ch[v][0]] = jmp[v];
        }
        for &w in &ch[v] {
            Self::dfs(ch, st, w, cnt, jmp);
        }
    }
    pub fn new(ch: &mut [Vec<usize>], par: &[usize], root: usize) -> Self {
        let n = ch.len();
        let mut st = vec![0; n];
        let mut cnt = 0;
        let mut sz = vec![0; n];
        let mut jmp = vec![0; n];
        let mut dep = vec![0; n];
        Self::dfs_left(ch, root, &mut sz, &mut dep, 0);
        for i in 0..n {
            jmp[i] = i;
        }
        Self::dfs(ch, &mut st, root, &mut cnt, &mut jmp);
        LCA {
            st: st,
            par: par.to_vec(),
            jmp: jmp,
            dep: dep,
        }
    }
    pub fn lca(&self, mut x: usize, mut y: usize) -> usize {
        let jmp = &self.jmp;
        let st = &self.st;
        while jmp[x] != jmp[y] {
            if st[x] < st[y] {
                std::mem::swap(&mut x, &mut y);
            }
            x = self.par[jmp[x]];
        }
        if st[x] < st[y] {
            x
        } else {
            y
        }
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn dfs(v: usize, par: usize, g: &[Vec<(usize, i64)>], ch: &mut [Vec<usize>], pars: &mut [usize], dist: &mut [i64], d: i64) {
    dist[v] = d;
    for &(w, c) in &g[v] {
        if w == par {
            pars[v] = w;
            continue;
        }
        ch[v].push(w);
        dfs(w, v, g, ch, pars, dist, d + c);
    }
}

fn solve() {
    input! {
        n: usize, q: usize, c: i64,
        uvl: [(usize1, usize1, i64); n - 1],
        x: [usize1; q],
    }
    let mut g = vec![vec![]; n];
    for (u, v, l) in uvl {
        g[u].push((v, l));
        g[v].push((u, l));
    }
    let mut ch = vec![vec![]; n];
    let mut pars = vec![0; n];
    let mut dist = vec![0; n];
    dfs(0, n, &g, &mut ch, &mut pars, &mut dist, 0);
    let lca = LCA::new(&mut ch, &pars, 0);
    const INF: i64 = 1 << 60;
    let mut dp = vec![INF; n];
    let mut dpn = 0;
    for i in 1..q {
        let mut ep = vec![INF; n];
        let mut epn = INF;
        let mut que = BinaryHeap::new();
        for i in 0..n {
            que.push((Reverse(dp[i]), i));
        }
        que.push((Reverse(dpn - c), x[i - 1]));
        while let Some((Reverse(d), v)) = que.pop() {
            if ep[v] <= d { continue; }
            ep[v] = d;
            for &(w, c) in &g[v] {
                que.push((Reverse(d + c), w));
            }
        }
        let mut dist = vec![INF; n];
        let mut que = VecDeque::new();
        que.push_back((0, x[i]));
        while let Some((d, v)) = que.pop_front() {
            if dist[v] <= d { continue; }
            dist[v] = d;
            for &(w, c) in &g[v] {
                que.push_back((d + c, w));
            }
        }
        for i in 0..n {
            ep[i] += c + dist[i];
            epn = min(epn, dp[i] + c + dist[i]);
        }
        let distx = dist[x[i - 1]];
        epn = min(epn, dpn + distx);
        for i in 0..n {
            ep[i] = min(ep[i], dp[i] + distx);
        }
        dp = ep;
        dpn = epn;
        // eprintln!("{:?} {}", dp, dpn);
    }
    println!("{}", dpn);
}
0