結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー phsplsphspls
提出日時 2022-12-29 01:34:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 2,313 bytes
コンパイル時間 14,289 ms
コンパイル使用メモリ 378,560 KB
実行使用メモリ 26,624 KB
最終ジャッジ日時 2024-05-03 06:19:44
合計ジャッジ時間 21,497 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 131 ms
14,464 KB
testcase_03 AC 126 ms
23,040 KB
testcase_04 AC 122 ms
23,040 KB
testcase_05 AC 100 ms
22,784 KB
testcase_06 AC 100 ms
22,912 KB
testcase_07 AC 28 ms
8,320 KB
testcase_08 AC 103 ms
26,368 KB
testcase_09 AC 10 ms
5,376 KB
testcase_10 AC 45 ms
12,800 KB
testcase_11 AC 31 ms
9,344 KB
testcase_12 AC 21 ms
5,504 KB
testcase_13 AC 100 ms
14,464 KB
testcase_14 AC 122 ms
17,920 KB
testcase_15 AC 146 ms
20,864 KB
testcase_16 AC 60 ms
9,856 KB
testcase_17 AC 170 ms
22,016 KB
testcase_18 AC 40 ms
7,552 KB
testcase_19 AC 149 ms
21,504 KB
testcase_20 AC 33 ms
7,424 KB
testcase_21 AC 49 ms
8,832 KB
testcase_22 AC 135 ms
19,328 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 18 ms
5,376 KB
testcase_26 AC 71 ms
12,288 KB
testcase_27 AC 73 ms
11,520 KB
testcase_28 AC 134 ms
17,280 KB
testcase_29 AC 13 ms
5,376 KB
testcase_30 AC 152 ms
21,120 KB
testcase_31 AC 100 ms
16,384 KB
testcase_32 AC 61 ms
11,264 KB
testcase_33 AC 150 ms
21,888 KB
testcase_34 AC 46 ms
7,680 KB
testcase_35 AC 154 ms
20,992 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 239 ms
26,624 KB
testcase_42 AC 55 ms
9,472 KB
testcase_43 AC 103 ms
15,104 KB
testcase_44 AC 30 ms
6,656 KB
testcase_45 AC 106 ms
14,720 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 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