結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー koba-e964koba-e964
提出日時 2020-05-29 23:47:57
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 4,042 bytes
コンパイル時間 2,918 ms
コンパイル使用メモリ 172,160 KB
実行使用メモリ 29,696 KB
最終ジャッジ日時 2024-04-24 02:15:31
合計ジャッジ時間 6,153 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 68 ms
16,000 KB
testcase_03 AC 94 ms
26,240 KB
testcase_04 AC 91 ms
25,984 KB
testcase_05 AC 77 ms
27,008 KB
testcase_06 AC 78 ms
27,008 KB
testcase_07 AC 22 ms
9,216 KB
testcase_08 AC 84 ms
29,568 KB
testcase_09 AC 8 ms
5,376 KB
testcase_10 AC 35 ms
14,208 KB
testcase_11 AC 25 ms
10,240 KB
testcase_12 AC 14 ms
7,168 KB
testcase_13 AC 61 ms
16,768 KB
testcase_14 AC 80 ms
20,352 KB
testcase_15 AC 93 ms
23,680 KB
testcase_16 AC 40 ms
11,648 KB
testcase_17 AC 116 ms
25,216 KB
testcase_18 AC 29 ms
9,088 KB
testcase_19 AC 103 ms
24,192 KB
testcase_20 AC 24 ms
8,192 KB
testcase_21 AC 34 ms
11,008 KB
testcase_22 AC 88 ms
22,016 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 16 ms
6,912 KB
testcase_26 AC 55 ms
13,952 KB
testcase_27 AC 46 ms
13,312 KB
testcase_28 AC 85 ms
20,352 KB
testcase_29 AC 10 ms
5,376 KB
testcase_30 AC 113 ms
23,808 KB
testcase_31 AC 69 ms
18,304 KB
testcase_32 AC 41 ms
12,800 KB
testcase_33 AC 111 ms
24,192 KB
testcase_34 AC 29 ms
9,344 KB
testcase_35 AC 103 ms
23,936 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 1 ms
5,376 KB
testcase_41 AC 164 ms
29,696 KB
testcase_42 AC 35 ms
10,624 KB
testcase_43 AC 75 ms
16,768 KB
testcase_44 AC 21 ms
7,424 KB
testcase_45 AC 68 ms
16,256 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

/*
 * Dijkstra's algorithm.
 * Verified by: AtCoder ABC164 (https://atcoder.jp/contests/abc164/submissions/12423853)
 */

struct Dijkstra {
    edges: Vec<Vec<(usize, i64)>>, // adjacent list representation
}

impl Dijkstra {
    fn new(n: usize) -> Self {
        Dijkstra { edges: vec![Vec::new(); n] }
    }
    fn add_edge(&mut self, from: usize, to: usize, cost: i64) {
        self.edges[from].push((to, cost));
    }
    /*
     * This function returns a Vec consisting of the distances from vertex source.
     */
    fn solve(&self, source: usize, inf: i64) -> Vec<i64> {
        let n = self.edges.len();
        let mut d = vec![inf; n];
        // que holds (-distance, vertex), so that que.pop() returns the nearest element.
        let mut que = std::collections::BinaryHeap::new();
        que.push((0, source));
        while let Some((cost, pos)) = que.pop() {
            let cost = -cost;
            if d[pos] <= cost {
                continue;
            }
            d[pos] = cost;
            for &(w, c) in &self.edges[pos] {
                let newcost = cost + c;
                if d[w] > newcost {
                    d[w] = newcost + 1;
                    que.push((-newcost, w));
                }
            }
        }
        return d;
    }
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (let _ = write!(out,$($format)*););
    }
    input! {
        n: usize, m: usize,
        vx: usize1, vy: usize1,
        xy: [(i64, i64); n],
        pq: [(usize1, usize1); m],
    }
    let mut dijk = Dijkstra::new(n);
    for &(p, q) in &pq {
        let (x1, y1) = xy[p];
        let (x2, y2) = xy[q];
        let dist = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
        let dist = (dist as f64 * 1.0e10).sqrt() as i64;
        dijk.add_edge(p, q, dist);
        dijk.add_edge(q, p, dist);
    }
    let sol = dijk.solve(vx, 1 << 58);
    puts!("{}\n", sol[vy] as f64 / 100000.0);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0