結果
問題 | No.614 壊れたキャンパス |
ユーザー | hatoo |
提出日時 | 2018-01-12 20:06:52 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 142 ms / 2,000 ms |
コード長 | 3,351 bytes |
コンパイル時間 | 11,894 ms |
コンパイル使用メモリ | 400,696 KB |
実行使用メモリ | 26,828 KB |
最終ジャッジ日時 | 2024-06-06 10:08:35 |
合計ジャッジ時間 | 14,420 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,944 KB |
testcase_08 | AC | 69 ms
11,784 KB |
testcase_09 | AC | 142 ms
18,912 KB |
testcase_10 | AC | 62 ms
16,496 KB |
testcase_11 | AC | 78 ms
11,396 KB |
testcase_12 | AC | 86 ms
11,500 KB |
testcase_13 | AC | 79 ms
11,404 KB |
testcase_14 | AC | 75 ms
11,660 KB |
testcase_15 | AC | 46 ms
14,068 KB |
testcase_16 | AC | 70 ms
13,720 KB |
testcase_17 | AC | 82 ms
26,828 KB |
testcase_18 | AC | 61 ms
14,844 KB |
testcase_19 | AC | 49 ms
15,008 KB |
ソースコード
#[allow(unused_imports)] use std::cmp::{max, min, Ordering}; #[allow(unused_imports)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; #[allow(unused_imports)] use std::iter::FromIterator; #[allow(unused_imports)] use std::io::stdin; mod util { use std::io::stdin; use std::str::FromStr; use std::fmt::Debug; #[allow(dead_code)] pub fn line() -> String { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.trim().to_string() } #[allow(dead_code)] pub fn gets<T: FromStr>() -> Vec<T> where <T as FromStr>::Err: Debug, { let mut line: String = String::new(); stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse().unwrap()) .collect() } } #[allow(unused_macros)] macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; } #[allow(unused_macros)] macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } } fn main() { let (n, m, _k, s, t) = get!(usize, usize, i64, i64, i64); let abc = get!(usize, i64, i64; m); let mut edges = vec![Vec::new(); n - 1]; for &(a, b, c) in &abc { edges[a - 1].push((b, c)); } let mut dp = BTreeMap::new(); dp.insert(s, 0); for es in edges.iter_mut() { if es.is_empty() { println!("-1"); return; } es.sort(); let v: Vec<(i64, i64)> = dp.into_iter().collect(); dp = BTreeMap::new(); let mut i = v.len(); let mut cost = i64::max_value() / 4; for &(b, c) in es.iter().rev() { while i > 0 && v[i - 1].0 >= b { i -= 1; cost = min(cost, v[i].0 + v[i].1); } let nc = cost - b; let v = dp.entry(c).or_insert(nc); *v = min(*v, nc); } let mut i = 0; let mut cost = i64::max_value() / 4; for &(b, c) in es.iter() { while i < v.len() && v[i].0 <= b { cost = min(cost, v[i].1 - v[i].0); i += 1; } let nc = cost + b; let v = dp.entry(c).or_insert(nc); *v = min(*v, nc); } } println!( "{}", dp.into_iter() .map(|(p, c)| c + (t - p).abs()) .min() .unwrap() ); }