結果

問題 No.2739 Time is money
ユーザー Yukino DX.Yukino DX.
提出日時 2024-04-26 22:40:31
言語 Rust
(1.77.0)
結果
AC  
実行時間 208 ms / 2,000 ms
コード長 2,039 bytes
コンパイル時間 4,873 ms
コンパイル使用メモリ 164,864 KB
実行使用メモリ 35,456 KB
最終ジャッジ日時 2024-04-26 22:40:42
合計ジャッジ時間 6,643 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 0 ms
5,376 KB
testcase_02 AC 65 ms
21,248 KB
testcase_03 AC 138 ms
27,136 KB
testcase_04 AC 51 ms
20,480 KB
testcase_05 AC 83 ms
19,840 KB
testcase_06 AC 108 ms
24,448 KB
testcase_07 AC 187 ms
35,456 KB
testcase_08 AC 186 ms
35,456 KB
testcase_09 AC 184 ms
34,688 KB
testcase_10 AC 109 ms
34,432 KB
testcase_11 AC 208 ms
34,816 KB
testcase_12 AC 129 ms
34,944 KB
testcase_13 AC 132 ms
35,072 KB
testcase_14 AC 125 ms
34,304 KB
testcase_15 AC 72 ms
29,312 KB
testcase_16 AC 73 ms
27,264 KB
testcase_17 AC 170 ms
33,536 KB
testcase_18 AC 175 ms
34,432 KB
testcase_19 AC 99 ms
29,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![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>
    where
        F: Fn(usize, usize) -> Vec<(usize, usize)>, //(crr_pos,crr_dist)->Vec<(nxt_pos,nxt_dist)>
    {
        neigh: F,
    }

    impl<F> Dijkstra<F>
    where
        F: 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;
}
0