結果
問題 | No.1065 電柱 / Pole (Easy) |
ユーザー |
|
提出日時 | 2022-12-29 01:34:24 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 233 ms / 2,000 ms |
コード長 | 2,313 bytes |
コンパイル時間 | 17,904 ms |
コンパイル使用メモリ | 377,884 KB |
実行使用メモリ | 26,624 KB |
最終ジャッジ日時 | 2024-11-24 04:02:37 |
合計ジャッジ時間 | 21,440 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 46 |
ソースコード
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]); }