結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー phsplsphspls
提出日時 2022-12-29 01:34:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 2,313 bytes
コンパイル時間 3,473 ms
コンパイル使用メモリ 154,656 KB
実行使用メモリ 26,624 KB
最終ジャッジ日時 2023-08-15 19:35:37
合計ジャッジ時間 8,345 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 78 ms
14,596 KB
testcase_03 AC 107 ms
22,880 KB
testcase_04 AC 103 ms
22,972 KB
testcase_05 AC 84 ms
22,952 KB
testcase_06 AC 82 ms
22,976 KB
testcase_07 AC 25 ms
8,204 KB
testcase_08 AC 94 ms
26,332 KB
testcase_09 AC 9 ms
4,376 KB
testcase_10 AC 42 ms
12,700 KB
testcase_11 AC 29 ms
9,232 KB
testcase_12 AC 18 ms
5,608 KB
testcase_13 AC 75 ms
14,280 KB
testcase_14 AC 95 ms
17,836 KB
testcase_15 AC 113 ms
20,960 KB
testcase_16 AC 49 ms
9,828 KB
testcase_17 AC 132 ms
22,088 KB
testcase_18 AC 34 ms
7,556 KB
testcase_19 AC 125 ms
21,664 KB
testcase_20 AC 27 ms
7,416 KB
testcase_21 AC 43 ms
8,956 KB
testcase_22 AC 113 ms
19,276 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 17 ms
5,288 KB
testcase_26 AC 62 ms
12,424 KB
testcase_27 AC 56 ms
11,268 KB
testcase_28 AC 99 ms
17,188 KB
testcase_29 AC 12 ms
4,376 KB
testcase_30 AC 118 ms
21,164 KB
testcase_31 AC 80 ms
16,440 KB
testcase_32 AC 50 ms
11,284 KB
testcase_33 AC 126 ms
21,776 KB
testcase_34 AC 37 ms
7,816 KB
testcase_35 AC 112 ms
21,060 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 168 ms
26,624 KB
testcase_42 AC 40 ms
9,568 KB
testcase_43 AC 77 ms
15,128 KB
testcase_44 AC 24 ms
6,804 KB
testcase_45 AC 74 ms
14,580 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const INF: f64 = (1usize << 60) as f64;

#[derive(PartialEq, PartialOrd, Clone, Debug)]
struct Sortable<T>(T);
 
impl<T: PartialEq> Eq for Sortable<T> {}
impl<T: PartialOrd> Ord for Sortable<T> {
    fn cmp(&self, other: &Sortable<T>) -> std::cmp::Ordering {
        self.0.partial_cmp(&other.0).unwrap()
    }
}


fn dijkstra(start: usize, paths: &Vec<Vec<(usize, f64)>>) -> Vec<f64> {
    let n = paths.len();
    let mut que = BinaryHeap::new();
    que.push((Reverse(Sortable(0.)), start));
    let mut costs = vec![INF; n];
    costs[start] = 0.;
    while let Some((Reverse(Sortable(cost)), u)) = que.pop() {
        if cost > costs[u] { continue; }
        for &(v, w) in paths[u].iter() {
            if costs[u] + w < costs[v] {
                costs[v] = costs[u] + w;
                que.push((Reverse(Sortable(costs[v])), v));
            }
        }
    }
    costs
}

fn calc(a: (isize, isize), b: (isize, isize)) -> f64 {
    (((a.0 - b.0) as f64).powf(2.) + ((a.1 - b.1) as f64).powf(2.)).sqrt()
}

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 temp = String::new();
    std::io::stdin().read_line(&mut temp).ok();
    let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let x = temp[0]-1;
    let y = temp[1]-1;
    let points = (0..n).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: Vec<isize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
            (temp[0], temp[1])
        })
        .collect::<Vec<_>>();
    let mut paths = vec![vec![]; n];
    for _ in 0..m {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let u = temp[0]-1;
        let v = temp[1]-1;
        let dist = calc(points[u], points[v]);
        paths[u].push((v, dist));
        paths[v].push((u, dist));
    }

    let dists = dijkstra(x, &paths);
    println!("{}", dists[y]);
}
0