結果
| 問題 |
No.1308 ジャンプビーコン
|
| コンテスト | |
| ユーザー |
fukafukatani
|
| 提出日時 | 2020-12-27 10:19:39 |
| 言語 | Rust (1.83.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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
コンパイルメッセージ
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`
ソースコード
#![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
}
fukafukatani