結果
問題 | No.2739 Time is money |
ユーザー |
|
提出日時 | 2024-04-26 22:40:31 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 167 ms / 2,000 ms |
コード長 | 2,039 bytes |
コンパイル時間 | 14,182 ms |
コンパイル使用メモリ | 379,728 KB |
実行使用メモリ | 35,584 KB |
最終ジャッジ日時 | 2024-11-14 14:29:23 |
合計ジャッジ時間 | 20,055 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#![allow(unused_imports)]//use itertools::{iproduct, Itertools};use proconio::input;use proconio::marker::*;use std::collections::*;fn main() {input! {n:usize,m:usize,x:usize,uvct:[(Usize1,Usize1,usize,usize);m],}let mut g = vec![vec![]; n];for &(u, v, c, t) in uvct.iter() {g[u].push((v, t * x + c));g[v].push((u, t * x + c));}let neigh = |crr_pos: usize, crr_dist: usize| {let mut nxts = vec![];for &(nxt, cost) in g[crr_pos].iter() {nxts.push((nxt, crr_dist + cost));}nxts};let dists = Dijkstra::new(neigh).solve(n, 0);if dists[n - 1] == INF {println!("-1");} else {let ans = (dists[n - 1] + x - 1) / x;println!("{}", ans);}}const INF: usize = std::usize::MAX;use dijkstra::*;mod dijkstra {use std::cmp::Reverse;pub struct Dijkstra<F>whereF: Fn(usize, usize) -> Vec<(usize, usize)>, //(crr_pos,crr_dist)->Vec<(nxt_pos,nxt_dist)>{neigh: F,}impl<F> Dijkstra<F>whereF: Fn(usize, usize) -> Vec<(usize, usize)>,{pub fn new(neigh: F) -> Self {Self { neigh }}pub fn solve(&self, n: usize, start: usize) -> Vec<usize> {let mut dists = vec![INF; n];dists[start] = 0;let mut pq = std::collections::BinaryHeap::new();pq.push(Reverse((0, start)));while let Some(Reverse((crr_dist, crr_pos))) = pq.pop() {if dists[crr_pos] < crr_dist {continue;}for (nxt_pos, nxt_dist) in (self.neigh)(crr_pos, crr_dist) {if nxt_dist < dists[nxt_pos] {dists[nxt_pos] = nxt_dist;pq.push(Reverse((nxt_dist, nxt_pos)));}}}dists}}const INF: usize = std::usize::MAX;}