結果

問題 No.1308 ジャンプビーコン
ユーザー fukafukatanifukafukatani
提出日時 2020-12-27 10:19:39
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 2,229 ms / 4,000 ms
コード長 4,187 bytes
コンパイル時間 14,728 ms
コンパイル使用メモリ 388,276 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-01 06:35:04
合計ジャッジ時間 45,605 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 0 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 2 ms
5,248 KB
testcase_09 AC 3 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 102 ms
5,248 KB
testcase_14 AC 103 ms
5,248 KB
testcase_15 AC 100 ms
5,248 KB
testcase_16 AC 100 ms
5,248 KB
testcase_17 AC 100 ms
5,248 KB
testcase_18 AC 2,229 ms
5,248 KB
testcase_19 AC 1,806 ms
5,248 KB
testcase_20 AC 1,704 ms
5,248 KB
testcase_21 AC 1,678 ms
5,248 KB
testcase_22 AC 1,679 ms
5,248 KB
testcase_23 AC 1 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 1 ms
5,248 KB
testcase_26 AC 1,388 ms
5,248 KB
testcase_27 AC 1,365 ms
5,248 KB
testcase_28 AC 1,400 ms
5,248 KB
testcase_29 AC 1,524 ms
5,248 KB
testcase_30 AC 1,534 ms
5,248 KB
testcase_31 AC 1,375 ms
5,248 KB
testcase_32 AC 1,498 ms
5,248 KB
testcase_33 AC 1,698 ms
5,248 KB
testcase_34 AC 1,359 ms
5,248 KB
testcase_35 AC 1,394 ms
5,248 KB
testcase_36 AC 1,513 ms
5,248 KB
testcase_37 AC 1,431 ms
5,248 KB
testcase_38 AC 1,880 ms
5,248 KB
testcase_39 AC 1,857 ms
5,248 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `q`
  --> src/main.rs:20:13
   |
20 |     let (n, q, c) = (v[0] as usize, v[1] as usize, v[2]);
   |             ^ help: if this is intentional, prefix it with an underscore: `_q`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `i`
  --> src/main.rs:24:9
   |
24 |     for i in 0..n - 1 {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i64>();
    let (n, q, c) = (v[0] as usize, v[1] as usize, v[2]);

    let mut edges = vec![vec![]; n];
    let mut graph = vec![vec![]; n];
    for i in 0..n - 1 {
        let v = read_vec::<i64>();
        let (a, b, c) = (v[0] as usize - 1, v[1] as usize - 1, v[2]);
        edges[a].push(Edge { to: b, cost: c });
        edges[b].push(Edge { to: a, cost: c });
        graph[a].push(b);
        graph[b].push(a);
    }

    let x = read_vec::<usize>()
        .iter()
        .map(|&x| x - 1)
        .collect::<Vec<_>>();

    let mut dp = vec![std::i64::MAX; n];
    let mut cur = x[0];
    dp[cur] = 0;
    for to in x.into_iter().skip(1) {
        let d = solve(&edges, to);
        let mut next = vec![std::i64::MAX; n];
        for i in 0..n {
            if dp[i] == std::i64::MAX {
                continue;
            }
            next[i] = min(dp[i] + d[cur], dp[i] + c + d[i]);
        }

        let mut tree: Vec<Vec<usize>> = vec![Vec::new(); n];
        let mut parent_dict: HashMap<usize, usize> = HashMap::new();
        make_tree(to, n, &graph, &mut tree, &mut parent_dict);
        let mut topological_sorted_indexes = vec![to];
        topological_dfs(to, &tree, &mut topological_sorted_indexes);

        for &ti in topological_sorted_indexes.iter().rev() {
            if ti == to {
                break;
            }
            if next[ti] == std::i64::MAX {
                continue;
            }
            let x = parent_dict[&ti];
            next[x] = min(next[x], next[ti]);
        }
        // debug!(next);

        cur = to;
        dp = next;
    }
    let ans = dp.iter().min().unwrap();
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn make_tree(
    cur_idx: usize,
    parent_idx: usize,
    edges: &Vec<Vec<usize>>,
    tree: &mut Vec<Vec<usize>>,
    parent_dict: &mut HashMap<usize, usize>,
) {
    for child_idx in edges[cur_idx].iter() {
        if *child_idx == parent_idx {
            continue;
        }
        tree[cur_idx].push(*child_idx);
        parent_dict.insert(*child_idx, cur_idx);
        make_tree(*child_idx, cur_idx, edges, tree, parent_dict);
    }
}

fn topological_dfs(cur_idx: usize, tree: &Vec<Vec<usize>>, result: &mut Vec<usize>) {
    for child_idx in tree[cur_idx].iter() {
        result.push(*child_idx);
        topological_dfs(*child_idx, tree, result);
    }
}

type Cost = i64;

const INF: Cost = 100000_00000_00000;

#[derive(PartialEq, Debug)]
struct MinInt {
    value: Cost,
}

impl Eq for MinInt {}

impl PartialOrd for MinInt {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.value.partial_cmp(&self.value)
    }
}

impl Ord for MinInt {
    fn cmp(&self, other: &MinInt) -> Ordering {
        other.value.partial_cmp(&self.value).unwrap()
    }
}

fn make_pair(x: Cost, y: usize) -> (MinInt, usize) {
    (MinInt { value: x }, y)
}

#[derive(Debug, Clone)]
struct Edge {
    to: usize,
    cost: Cost,
}

fn solve(edges: &Vec<Vec<Edge>>, start_idx: usize) -> Vec<Cost> {
    let num_apexes = edges.len();
    let mut d = vec![INF; num_apexes];
    d[start_idx] = 0;
    let mut que = BinaryHeap::new();
    que.push(make_pair(0, start_idx));

    while let Some((u, v)) = que.pop() {
        if d[v] < u.value {
            continue;
        }
        for e in &edges[v] {
            if d[v] != INF && d[e.to] > d[v] + e.cost {
                d[e.to] = d[v] + e.cost;
                que.push(make_pair(d[e.to], e.to));
            }
        }
    }
    d
}
0