結果
問題 | No.2739 Time is money |
ユーザー | naut3 |
提出日時 | 2024-04-24 00:17:30 |
言語 | Rust (1.77.0 + proconio) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,245 bytes |
コンパイル時間 | 13,809 ms |
コンパイル使用メモリ | 402,512 KB |
実行使用メモリ | 36,472 KB |
最終ジャッジ日時 | 2024-11-06 06:47:08 |
合計ジャッジ時間 | 21,304 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
13,636 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 60 ms
19,328 KB |
testcase_03 | AC | 169 ms
27,668 KB |
testcase_04 | AC | 54 ms
18,560 KB |
testcase_05 | AC | 102 ms
19,368 KB |
testcase_06 | AC | 124 ms
24,836 KB |
testcase_07 | AC | 235 ms
35,348 KB |
testcase_08 | AC | 195 ms
35,544 KB |
testcase_09 | AC | 199 ms
35,276 KB |
testcase_10 | AC | 111 ms
31,484 KB |
testcase_11 | AC | 193 ms
34,464 KB |
testcase_12 | AC | 127 ms
35,660 KB |
testcase_13 | AC | 131 ms
35,628 KB |
testcase_14 | AC | 125 ms
36,444 KB |
testcase_15 | TLE | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
ソースコード
#![allow(non_snake_case, unused_imports, unused_must_use)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; ($T: ty, $N: expr) => { (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>() }; } let N = input!(usize); let M = input!(usize); let X = input!(usize); let mut edges = vec![]; for _ in 0..M { let u = input!(usize); let v = input!(usize); let c = input!(usize); let t = input!(usize); edges.push((u - 1, v - 1, c + t * X)); edges.push((v - 1, u - 1, c + t * X)); } let dist = dijkstras_algorithm(N, &edges, 0)[N - 1]; match dist { Some(d) => { writeln!(out, "{}", (d + X - 1) / X); } None => { writeln!(out, "-1"); } } } pub fn dijkstras_algorithm< W: Sized + std::ops::Add<Output = W> + PartialOrd + Ord + Default + Clone + Copy, >( size: usize, directed_edges: &[(usize, usize, W)], start: usize, ) -> Vec<Option<W>> { assert!(start < size); let graph = { let mut g = vec![vec![]; size]; for &(from, to, weight) in directed_edges.iter() { assert!(from < size && to < size); g[from].push((to, weight)); } g }; let mut dist = vec![None; size]; dist[start] = Some(W::default()); let mut hq = std::collections::BinaryHeap::new(); hq.push((std::cmp::Reverse(W::default()), start)); while let Some((_, now)) = hq.pop() { for &(nxt, weight) in graph[now].iter() { match dist[nxt] { Some(prev_dist) => { if dist[now].unwrap() + weight < prev_dist { let d = dist[now].unwrap() + weight; dist[nxt] = Some(d); hq.push((std::cmp::Reverse(d), nxt)); } } None => { let d = dist[now].unwrap() + weight; dist[nxt] = Some(d); hq.push((std::cmp::Reverse(d), nxt)); } } } } dist } struct Scanner<R> { reader: R, buf_str: Vec<u8>, buf_iter: str::SplitWhitespace<'static>, } impl<R: BufRead> Scanner<R> { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token<T: str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }