結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー StrorkisStrorkis
提出日時 2020-05-29 23:57:36
言語 Rust
(1.77.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 2,074 bytes
コンパイル時間 1,261 ms
コンパイル使用メモリ 155,520 KB
実行使用メモリ 30,788 KB
最終ジャッジ日時 2024-04-24 02:32:56
合計ジャッジ時間 5,183 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 80 ms
16,384 KB
testcase_03 AC 87 ms
26,240 KB
testcase_04 AC 82 ms
25,984 KB
testcase_05 AC 56 ms
25,472 KB
testcase_06 AC 55 ms
25,344 KB
testcase_07 AC 17 ms
9,344 KB
testcase_08 AC 60 ms
30,720 KB
testcase_09 AC 6 ms
5,376 KB
testcase_10 AC 27 ms
14,592 KB
testcase_11 AC 19 ms
10,496 KB
testcase_12 AC 10 ms
6,272 KB
testcase_13 AC 71 ms
16,128 KB
testcase_14 AC 93 ms
19,968 KB
testcase_15 AC 116 ms
23,276 KB
testcase_16 AC 44 ms
11,136 KB
testcase_17 AC 135 ms
24,576 KB
testcase_18 AC 29 ms
8,576 KB
testcase_19 AC 121 ms
24,064 KB
testcase_20 AC 25 ms
7,936 KB
testcase_21 AC 32 ms
10,112 KB
testcase_22 AC 107 ms
21,632 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 9 ms
6,016 KB
testcase_26 AC 54 ms
13,568 KB
testcase_27 AC 52 ms
12,800 KB
testcase_28 AC 99 ms
19,584 KB
testcase_29 AC 8 ms
5,376 KB
testcase_30 AC 114 ms
23,424 KB
testcase_31 AC 76 ms
18,048 KB
testcase_32 AC 46 ms
12,416 KB
testcase_33 AC 115 ms
24,064 KB
testcase_34 AC 31 ms
8,832 KB
testcase_35 AC 112 ms
23,552 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 182 ms
30,788 KB
testcase_42 AC 36 ms
10,624 KB
testcase_43 AC 85 ms
17,024 KB
testcase_44 AC 23 ms
7,552 KB
testcase_45 AC 77 ms
16,640 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;
use std::cmp::Ordering;
use std::collections::BinaryHeap;

#[derive(Copy, Clone)]
struct State {
    cost: f64,
    pos: usize,
}

impl PartialOrd for State {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.cost.partial_cmp(&self.cost)
    }
}

impl PartialEq for State {
    fn eq(&self, other: &Self) -> bool {
        self.cost == other.cost
    }
}

impl Ord for State {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(&other).unwrap()
    }
}

impl Eq for State {}

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut iter = buf.split_whitespace();

    let n: usize = iter.next().unwrap().parse().unwrap();
    let m: usize = iter.next().unwrap().parse().unwrap();

    let x = iter.next().unwrap().parse::<usize>().unwrap() - 1;
    let y = iter.next().unwrap().parse::<usize>().unwrap() - 1;

    let mut pole: Vec<(i64, i64)> = Vec::with_capacity(n);
    for _ in 0..n {
        let p: i64 = iter.next().unwrap().parse().unwrap();
        let q: i64 = iter.next().unwrap().parse().unwrap();
        pole.push((p, q));
    }

    let mut g: Vec<Vec<(f64, usize)>> = vec![Vec::new(); n];
    for _ in 0..m {
        let i = iter.next().unwrap().parse::<usize>().unwrap() - 1;
        let j = iter.next().unwrap().parse::<usize>().unwrap() - 1;
        let dx = pole[i].0 - pole[j].0;
        let dy = pole[i].1 - pole[j].1;
        let cost = ((dx * dx + dy * dy) as f64).sqrt();
        g[i].push((cost, j));
        g[j].push((cost, i));
    }

    let mut dist: Vec<f64> = vec![std::f64::MAX; n];
    let mut heap = BinaryHeap::new();

    dist[x] = 0.0;
    heap.push(State { cost: 0.0, pos: x });

    while let Some(State { cost, pos }) = heap.pop() {
        for edge in &g[pos] {
            let next = State { cost: cost + edge.0, pos: edge.1};
            if next.cost < dist[next.pos] {
                heap.push(next);
                dist[next.pos] = next.cost;
            }
        }
    }

    println!("{}", dist[y]);
}
0