結果

問題 No.2909 Imaginary Summer
ユーザー ArcAkiArcAki
提出日時 2024-09-27 22:12:53
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 266 ms / 6,000 ms
コード長 6,519 bytes
コンパイル時間 14,528 ms
コンパイル使用メモリ 379,536 KB
実行使用メモリ 31,432 KB
最終ジャッジ日時 2024-10-02 17:42:41
合計ジャッジ時間 20,363 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 0 ms
5,248 KB
testcase_02 AC 0 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 82 ms
15,844 KB
testcase_10 AC 54 ms
10,572 KB
testcase_11 AC 48 ms
11,460 KB
testcase_12 AC 81 ms
15,980 KB
testcase_13 AC 58 ms
12,136 KB
testcase_14 AC 49 ms
9,724 KB
testcase_15 AC 165 ms
22,740 KB
testcase_16 AC 188 ms
19,596 KB
testcase_17 AC 179 ms
22,296 KB
testcase_18 AC 185 ms
19,244 KB
testcase_19 AC 163 ms
23,012 KB
testcase_20 AC 241 ms
30,768 KB
testcase_21 AC 238 ms
29,624 KB
testcase_22 AC 206 ms
26,732 KB
testcase_23 AC 234 ms
27,936 KB
testcase_24 AC 238 ms
30,108 KB
testcase_25 AC 216 ms
27,668 KB
testcase_26 AC 234 ms
28,364 KB
testcase_27 AC 252 ms
30,156 KB
testcase_28 AC 259 ms
31,176 KB
testcase_29 AC 263 ms
31,176 KB
testcase_30 AC 256 ms
31,432 KB
testcase_31 AC 258 ms
31,304 KB
testcase_32 AC 250 ms
31,308 KB
testcase_33 AC 250 ms
31,176 KB
testcase_34 AC 251 ms
31,180 KB
testcase_35 AC 253 ms
31,136 KB
testcase_36 AC 250 ms
31,172 KB
testcase_37 AC 255 ms
31,308 KB
testcase_38 AC 266 ms
31,304 KB
testcase_39 AC 258 ms
31,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::{collections::{HashSet, HashMap, BTreeSet, BTreeMap, BinaryHeap, VecDeque}, cmp::{Reverse, PartialOrd, Ordering, max, min}, mem::swap};

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($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:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[derive(Debug, PartialEq, PartialOrd)]
struct OrderedFloat(f64);

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

const INF: i64 = 1<<60;

#[derive(Debug, Copy, Clone)]
struct KDNode{
    point: (i64, i64),
    left: Option<usize>,
    right: Option<usize>,
    axis: usize,
    mi1: i64,
    mx1: i64,
    mi2: i64,
    mx2: i64,
}

pub struct KDTree{
    tree: Vec<KDNode>,
}

impl KDTree {
    pub fn new() -> Self{
        KDTree {tree: Vec::new()}
    }

    pub fn build(&mut self, points: &mut Vec<(i64, i64)>, d: usize)->Option<usize>{
        if points.is_empty(){return None;}
        let (mut mi1, mut mx1, mut mi2, mut mx2) = (INF, -INF, INF, -INF);
        for i in 0..points.len(){
            mi1 = mi1.min(points[i].0);
            mx1 = mx1.max(points[i].0);
            mi2 = mi2.min(points[i].1);
            mx2 = mx2.max(points[i].1);
        }
        let sort = points;
        let axis = if mx1-mi1 >= mx2-mi2{0} else {1};
        if axis==0{
            sort.sort_by(|a, b| a.0.cmp(&b.0));
        } else {
            sort.sort_by(|a, b| a.1.cmp(&b.1));
        }
        let m =sort.len()/2;
        let mp = sort[m].clone();
        let ind = self.tree.len();
        self.tree.push(KDNode{
            point: mp,
            left: None, right: None,
            axis,
            mi1, mx1, mi2, mx2
        });
        let left = self.build(&mut sort[..m].to_vec(), d+1);
        self.tree[ind].left = left;
        let right = self.build(&mut sort[m+1..].to_vec(), d+1);
        self.tree[ind].right = right;
        Some(ind)
    }

    pub fn query(&mut self, t: &(i64, i64), i: Option<usize>, res: &mut ((i64, i64), i64)){
        //println!("query: {:?} {:?} {:?}", t, i, res);
        if let Some(idx) = i{
            let node = self.tree[idx].point.clone();
            let axis = self.tree[idx].axis;
            let dist = dist_calc(&node, t);
            if dist < res.1{
                *res = (node.clone(), dist);
            }
            let (nex, other) = if (axis==0&&t.0 < node.0)||(axis==1&&t.1 < node.1){
                (self.tree[idx].left, self.tree[idx].right)
            } else {
                (self.tree[idx].right, self.tree[idx].left)
            };
            if let Some(n_idx) = nex{
                self.query(t, Some(n_idx), res);
            }
            if let Some(n_ind) = other{
                let (mix, mxx, miy, mxy) = (self.tree[n_ind].mi1, self.tree[n_ind].mx1, self.tree[n_ind].mi2, self.tree[n_ind].mx2);
                let ad = possible_dist(t.0, t.1, mix, mxx, miy, mxy);
                if ad < res.1{
                    self.query(t, Some(n_ind), res);
                }
            }
        }
    }
}

fn possible_dist(p: i64, q: i64, mix: i64, mxx: i64, miy: i64, mxy: i64)->i64{
    let (mix, mxx, miy, mxy) = (mix, mxx, miy, mxy);
    let f1 = mix <= p && p <= mxx;
    let f2 = miy <= q && q <= mxy;
    let x = if f1{0} else if p <= mix {mix-p} else {p-mxx};
    let y = if f2{0} else if q <= miy {miy-q} else {q-mxy};
    return x*x+y*y
}

fn dist_calc(p: &(i64, i64), q: &(i64, i64))->i64{
    let (x1, y1) = p;
    let (x2, y2) = q;
    (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)
}

fn dijkstra(n: usize, v: f64, p: usize, edge: &Vec<Vec<usize>>, s: &Vec<(i64, i64)>)->Vec<f64>{
    let mut time = vec![1e60; n];
    time[p] = 0.;
    let mut used = vec![false; n];
    let mut heap = BinaryHeap::new();
    heap.push(Reverse((OrderedFloat(0.), p)));
    while let Some(Reverse((OrderedFloat(w), x))) = heap.pop(){
        if used[x]{continue}
        used[x] = true;
        time[x] = w;
        for &nex in &edge[x]{
            if used[nex]{continue}
            let dx = (dist_calc(&s[nex], &s[x]) as f64).sqrt()/v;
            if time[nex] > w+dx{
                heap.push(Reverse((OrderedFloat(w+dx), nex)));
            }
        }
    }
    time
}

fn main() {
    input!{
    n: usize, m: usize, k: usize, 
    p: (i64, i64), 
    s: [(i64, i64); n],
    t: [(i64, i64); k],
    v: f64,
    e: [(usize1, usize1); m],
    }
    let mut s = s.clone();
    let mut edge = vec![Vec::new(); n];
    for &(u, v) in &e{
        edge[u].push(v);
        edge[v].push(u);
    }
    let mut dic = HashMap::new();
    let mut ans = 0.;
    let mut kdtree = KDTree::new();
    kdtree.build(&mut s, 0);
    let mut hotel = ((0, 0), INF);
    kdtree.query(&p, Some(0), &mut hotel);
    for i in 0..n{
        dic.insert((s[i].0, s[i].1), i);
    }
    let ss = dic[&hotel.0];
    let pre = (dist_calc(&p, &s[ss])as f64).sqrt();
    let time = dijkstra(n, v, ss, &edge, &s);
    for i in 0..k{
        let mut res = ((0, 0), INF);
        kdtree.query(&t[i], Some(0), &mut res);
        let d1 = (dist_calc(&t[i], &res.0) as f64).sqrt();
        let sub = time[dic[&res.0]];
        ans += ((dist_calc(&p, &t[i]) as f64).sqrt().min(d1+sub+pre))*2.;
    }
    println!("{}", ans);
}
0