結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー Strorkis
提出日時 2020-05-29 23:55:29
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 2,069 bytes
コンパイル時間 13,120 ms
コンパイル使用メモリ 388,424 KB
実行使用メモリ 30,864 KB
最終ジャッジ日時 2024-11-06 09:13:05
合計ジャッジ時間 17,779 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

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