結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 56 ms
16,384 KB
testcase_03 AC 72 ms
26,240 KB
testcase_04 AC 68 ms
26,112 KB
testcase_05 AC 49 ms
25,472 KB
testcase_06 AC 49 ms
25,472 KB
testcase_07 AC 14 ms
9,344 KB
testcase_08 AC 52 ms
30,592 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 23 ms
14,592 KB
testcase_11 AC 16 ms
10,496 KB
testcase_12 AC 8 ms
6,272 KB
testcase_13 AC 48 ms
16,128 KB
testcase_14 AC 66 ms
19,968 KB
testcase_15 AC 81 ms
23,296 KB
testcase_16 AC 31 ms
11,136 KB
testcase_17 AC 93 ms
24,576 KB
testcase_18 AC 20 ms
8,576 KB
testcase_19 AC 86 ms
23,936 KB
testcase_20 AC 19 ms
8,064 KB
testcase_21 AC 26 ms
10,240 KB
testcase_22 AC 73 ms
21,632 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 8 ms
6,016 KB
testcase_26 AC 38 ms
13,568 KB
testcase_27 AC 33 ms
12,800 KB
testcase_28 AC 63 ms
19,584 KB
testcase_29 AC 7 ms
5,376 KB
testcase_30 AC 78 ms
23,424 KB
testcase_31 AC 53 ms
18,176 KB
testcase_32 AC 30 ms
12,416 KB
testcase_33 AC 85 ms
24,064 KB
testcase_34 AC 23 ms
8,960 KB
testcase_35 AC 84 ms
23,424 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 136 ms
30,848 KB
testcase_42 AC 26 ms
10,624 KB
testcase_43 AC 49 ms
17,024 KB
testcase_44 AC 16 ms
7,552 KB
testcase_45 AC 51 ms
16,640 KB
testcase_46 AC 0 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![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