結果
問題 | No.1393 Median of Walk |
ユーザー | koba-e964 |
提出日時 | 2021-11-17 14:53:38 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 19 ms / 2,000 ms |
コード長 | 3,524 bytes |
コンパイル時間 | 13,965 ms |
コンパイル使用メモリ | 380,048 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-06 03:20:31 |
合計ジャッジ時間 | 15,821 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 19 ms
5,376 KB |
testcase_09 | AC | 19 ms
5,376 KB |
testcase_10 | AC | 18 ms
5,376 KB |
testcase_11 | AC | 7 ms
5,376 KB |
testcase_12 | AC | 7 ms
5,376 KB |
testcase_13 | AC | 7 ms
5,376 KB |
testcase_14 | AC | 7 ms
5,376 KB |
testcase_15 | AC | 7 ms
5,376 KB |
testcase_16 | AC | 6 ms
5,376 KB |
testcase_17 | AC | 6 ms
5,376 KB |
testcase_18 | AC | 6 ms
5,376 KB |
testcase_19 | AC | 6 ms
5,376 KB |
testcase_20 | AC | 6 ms
5,376 KB |
testcase_21 | AC | 7 ms
5,376 KB |
testcase_22 | AC | 7 ms
5,376 KB |
testcase_23 | AC | 6 ms
5,376 KB |
testcase_24 | AC | 7 ms
5,376 KB |
testcase_25 | AC | 6 ms
5,376 KB |
testcase_26 | AC | 17 ms
5,376 KB |
testcase_27 | AC | 18 ms
5,376 KB |
testcase_28 | AC | 1 ms
5,376 KB |
testcase_29 | AC | 4 ms
5,376 KB |
testcase_30 | AC | 7 ms
5,376 KB |
testcase_31 | AC | 7 ms
5,376 KB |
testcase_32 | AC | 5 ms
5,376 KB |
testcase_33 | AC | 4 ms
5,376 KB |
testcase_34 | AC | 5 ms
5,376 KB |
testcase_35 | AC | 5 ms
5,376 KB |
testcase_36 | AC | 5 ms
5,376 KB |
testcase_37 | AC | 1 ms
5,376 KB |
testcase_38 | AC | 3 ms
5,376 KB |
testcase_39 | AC | 3 ms
5,376 KB |
testcase_40 | AC | 9 ms
5,376 KB |
testcase_41 | AC | 3 ms
5,376 KB |
ソースコード
#[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, usize1) => (read_value!($next, usize) - 1); ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } fn main() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} input! { n: usize, m: usize, uvw: [(usize1, usize1, i64); m], } let mut uvw = uvw; uvw.sort_by_key(|x| x.2); let mut g = vec![vec![]; n]; for &(u, v, _) in &uvw { g[u].push((v, 1)); } const INF: i32 = 1 << 10; let mut h = vec![INF; n]; let mut que = VecDeque::new(); que.push_back((0, 0)); while let Some((d, v)) = que.pop_front() { if h[v] <= d { continue; } h[v] = d; for &(w, _) in &g[v] { que.push_back((d + 1, w)); } } let mut dist = vec![0; n]; let mut ans = vec![-1; n]; let mut que = BinaryHeap::new(); for &(u, v, w) in &uvw { if h[u] >= INF { continue; } let idx = g[u].iter().position(|x| x.0 == v).unwrap(); assert_eq!(g[u][idx], (v, 1)); g[u][idx].1 = -1; que.push((Reverse(max(-INF, dist[u] + h[u] - 1) - h[v]), v)); loop { let mut found = false; while let Some((Reverse(d), a)) = que.pop() { if a == u && d < dist[u] { found = true; break; } if dist[a] <= d { continue; } dist[a] = d; for &(w, c) in &g[a] { let nd = if d + h[a] == -INF { -INF - h[w] } else { d + c + h[a] - h[w] }; if dist[w] > nd { que.push((Reverse(nd), w)); } } } if !found { break; } h[u] = -INF; dist[u] = 0; que.clear(); for &(w, _) in &g[u] { que.push((Reverse(-INF - h[w]), w)); } } for i in 0..n { h[i] += dist[i]; dist[i] = 0; } for i in 0..n { if ans[i] == -1 && h[i] <= 0 { ans[i] = w; } } que.clear(); } for i in 1..n { puts!("{}\n", ans[i]); } }