結果

問題 No.1995 CHIKA Road
ユーザー phsplsphspls
提出日時 2022-09-20 00:26:37
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 3,243 bytes
コンパイル時間 13,725 ms
コンパイル使用メモリ 390,984 KB
実行使用メモリ 53,260 KB
最終ジャッジ日時 2024-06-01 16:36:53
合計ジャッジ時間 18,724 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 12 ms
6,940 KB
testcase_07 AC 48 ms
12,456 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 54 ms
10,368 KB
testcase_11 AC 319 ms
53,260 KB
testcase_12 AC 132 ms
29,832 KB
testcase_13 AC 85 ms
19,652 KB
testcase_14 AC 88 ms
20,520 KB
testcase_15 AC 274 ms
47,508 KB
testcase_16 AC 21 ms
7,052 KB
testcase_17 AC 113 ms
22,972 KB
testcase_18 AC 206 ms
37,088 KB
testcase_19 AC 210 ms
37,164 KB
testcase_20 AC 101 ms
21,168 KB
testcase_21 AC 90 ms
19,564 KB
testcase_22 AC 194 ms
33,312 KB
testcase_23 AC 48 ms
12,500 KB
testcase_24 AC 164 ms
29,488 KB
testcase_25 AC 26 ms
7,824 KB
testcase_26 AC 56 ms
13,976 KB
testcase_27 AC 157 ms
28,636 KB
testcase_28 AC 92 ms
19,540 KB
testcase_29 AC 206 ms
36,020 KB
testcase_30 AC 25 ms
7,660 KB
testcase_31 AC 12 ms
6,944 KB
testcase_32 AC 34 ms
9,428 KB
testcase_33 AC 150 ms
28,260 KB
testcase_34 AC 13 ms
6,944 KB
testcase_35 AC 54 ms
13,388 KB
testcase_36 AC 66 ms
15,028 KB
testcase_37 AC 183 ms
31,692 KB
testcase_38 AC 127 ms
24,288 KB
testcase_39 AC 10 ms
6,944 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, BTreeSet, HashMap}};

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 nm = String::new();
    std::io::stdin().read_line(&mut nm).ok();
    let nm: Vec<usize> = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nm[0];
    let m = nm[1];
    let mut lines = Vec::with_capacity(m);
    let mut used = BTreeSet::new();
    used.insert(0);
    used.insert(n-1);
    for _ in 0..m {
        let mut ab = String::new();
        std::io::stdin().read_line(&mut ab).ok();
        let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a = ab[0]-1;
        let b = ab[1]-1;
        let dist = 2*b - 2*a - 1;
        lines.push((a, b, dist));
        used.insert(a);
        used.insert(b);
    }
    let mut mapping = HashMap::new();
    for (i, &v) in used.iter().enumerate() {
        mapping.insert(v, i);
    }
    let mut paths = Dijkstra::new(used.len());
    for &(u, v, w) in lines.iter() {
        paths.pusha2b(*mapping.get(&u).unwrap(), *mapping.get(&v).unwrap(), w)
    }
    let used = used.into_iter().collect::<Vec<usize>>();
    for i in 0..used.len()-1 {
        let u = used[i];
        let v = used[i+1];
        let dist = (v - u) * 2;
        paths.pusha2b(i, i+1, dist);
    }
    paths.solve(0);
    println!("{}", paths.get_cost(used.len()-1));
}
0