結果

問題 No.1868 Teleporting Cyanmond
ユーザー phsplsphspls
提出日時 2022-09-25 16:45:18
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 2,456 bytes
コンパイル時間 11,071 ms
コンパイル使用メモリ 404,076 KB
実行使用メモリ 24,448 KB
最終ジャッジ日時 2024-06-01 22:23:12
合計ジャッジ時間 12,788 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 0 ms
6,940 KB
testcase_03 AC 43 ms
21,760 KB
testcase_04 AC 26 ms
16,512 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 7 ms
6,940 KB
testcase_07 AC 20 ms
11,776 KB
testcase_08 AC 13 ms
7,808 KB
testcase_09 AC 17 ms
10,624 KB
testcase_10 AC 42 ms
19,968 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 10 ms
6,944 KB
testcase_13 AC 18 ms
12,032 KB
testcase_14 AC 33 ms
22,528 KB
testcase_15 AC 20 ms
15,488 KB
testcase_16 AC 4 ms
6,944 KB
testcase_17 AC 8 ms
6,944 KB
testcase_18 AC 33 ms
24,320 KB
testcase_19 AC 32 ms
24,448 KB
testcase_20 AC 32 ms
24,448 KB
testcase_21 AC 33 ms
24,320 KB
testcase_22 AC 32 ms
24,428 KB
testcase_23 AC 31 ms
24,320 KB
testcase_24 AC 32 ms
24,320 KB
testcase_25 AC 31 ms
24,296 KB
testcase_26 AC 31 ms
24,192 KB
testcase_27 AC 30 ms
24,320 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> src/main.rs:7:5
  |
6 | struct Dijkstra {
  |        -------- field in this struct
7 |     n: usize,
  |     ^
  |
  = note: `Dijkstra` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

warning: method `reconstruct` is never used
  --> src/main.rs:49:8
   |
13 | impl Dijkstra {
   | ------------- method in this implementation
...
49 |     fn reconstruct(&self, startpoint: usize, endpoint: usize) -> Vec<(usize, usize)> {
   |        ^^^^^^^^^^^

ソースコード

diff #

use std::{cmp::Reverse, collections::BinaryHeap};

const INF: usize = 1usize << 60;

#[derive(Debug, Clone)]
struct Dijkstra {
    n: usize,
    pathcosts: Vec<Vec<(usize, usize)>>,
    pathcostsrev: Vec<Vec<(usize, usize)>>,
    costs: Vec<usize>
}

impl Dijkstra {
    fn new(n: usize) -> Self {
        Self {
              n: n
            , pathcosts: vec![vec![]; n]
            , pathcostsrev: vec![vec![]; n]
            , costs: vec![INF; n]
        }
    }

    fn pusha2b(&mut self, a: usize, b: usize, cost: usize) {
        self.pathcosts[a].push((cost, b));
        self.pathcostsrev[b].push((cost, a));
    }

    fn get_cost(&self, idx: usize) -> usize {
        self.costs[idx]
    }

    fn solve(&mut self, startpoint: usize) {
        let mut que = BinaryHeap::new();
        que.push(Reverse((0, startpoint)));
        self.costs[startpoint] = 0;
        while let Some(Reverse(p)) = que.pop() {
            let cost = p.0;
            let dest = p.1;
            if cost > self.costs[dest] { continue; }
            for &p2 in self.pathcosts[dest].iter() {
                if self.costs[dest] + p2.0 < self.costs[p2.1] {
                    self.costs[p2.1] = self.costs[dest] + p2.0;
                    que.push(Reverse((self.costs[p2.1], p2.1)));
                }
            }
        }
    }

    fn reconstruct(&self, startpoint: usize, endpoint: usize) -> Vec<(usize, usize)> {
        let mut ret = vec![];
        if self.costs[endpoint] == INF { return ret; }
        let mut current = endpoint;
        while current != startpoint {
            for &(cost, u) in self.pathcostsrev[current].iter() {
                if self.costs[current] == self.costs[u] + cost {
                    let prev = current;
                    current = u;
                    ret.push((current, prev));
                    break;
                }
            }
        }
        ret.reverse();
        ret
    }
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut r = String::new();
    std::io::stdin().read_line(&mut r).ok();
    let r: Vec<usize> = r.trim().split_whitespace().map(|s| s.parse::<usize>().unwrap()-1).collect();

    let mut dijkstra = Dijkstra::new(n);
    for i in 0..n-1 {
        dijkstra.pusha2b(i, r[i], 1);
        dijkstra.pusha2b(i+1, i, 0);
    }
    dijkstra.solve(0);
    println!("{}", dijkstra.get_cost(n-1));
}
0