結果

問題 No.2695 Warp Zone
ユーザー naut3naut3
提出日時 2024-03-22 22:21:49
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 3,707 bytes
コンパイル時間 12,129 ms
コンパイル使用メモリ 380,204 KB
実行使用メモリ 41,720 KB
最終ジャッジ日時 2024-09-30 11:53:34
合計ジャッジ時間 13,444 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,820 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 33 ms
41,720 KB
testcase_04 AC 31 ms
40,712 KB
testcase_05 AC 31 ms
40,436 KB
testcase_06 AC 32 ms
41,416 KB
testcase_07 AC 32 ms
41,716 KB
testcase_08 AC 1 ms
6,820 KB
testcase_09 AC 14 ms
17,188 KB
testcase_10 AC 1 ms
6,820 KB
testcase_11 AC 5 ms
6,816 KB
testcase_12 AC 18 ms
23,200 KB
testcase_13 AC 0 ms
6,816 KB
testcase_14 AC 22 ms
29,120 KB
testcase_15 AC 12 ms
15,104 KB
testcase_16 AC 4 ms
6,816 KB
testcase_17 AC 6 ms
9,088 KB
testcase_18 AC 25 ms
34,136 KB
testcase_19 AC 1 ms
6,820 KB
testcase_20 AC 25 ms
31,796 KB
testcase_21 AC 21 ms
29,596 KB
testcase_22 AC 8 ms
11,648 KB
testcase_23 AC 16 ms
22,952 KB
testcase_24 AC 0 ms
6,816 KB
testcase_25 AC 1 ms
6,816 KB
testcase_26 AC 0 ms
6,820 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `a1`
  --> src/main.rs:48:18
   |
48 |             let (a1, b1, c1, d1) = warp_points[i];
   |                  ^^ help: if this is intentional, prefix it with an underscore: `_a1`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `b1`
  --> src/main.rs:48:22
   |
48 |             let (a1, b1, c1, d1) = warp_points[i];
   |                      ^^ help: if this is intentional, prefix it with an underscore: `_b1`

warning: unused variable: `c2`
  --> src/main.rs:49:26
   |
49 |             let (a2, b2, c2, d2) = warp_points[j];
   |                          ^^ help: if this is intentional, prefix it with an underscore: `_c2`

warning: unused variable: `d2`
  --> src/main.rs:49:30
   |
49 |             let (a2, b2, c2, d2) = warp_points[j];
   |                              ^^ help: if this is intentional, prefix it with an underscore: `_d2`

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let H = input!(isize);
    let W = input!(isize);
    let N = input!(usize);

    let dist = |y1: isize, x1: isize, y2: isize, x2: isize| (y2 - y1).abs() + (x2 - x1).abs();

    let mut edges = vec![];
    edges.push((2 * N, 2 * N + 1, dist(H, W, 1, 1)));

    let mut warp_points = vec![];

    for _ in 0..N {
        let a = input!(isize);
        let b = input!(isize);
        let c = input!(isize);
        let d = input!(isize);
        warp_points.push((a, b, c, d));
    }

    for i in 0..N {
        let (a, b, c, d) = warp_points[i];

        edges.push((2 * N, 2 * i, dist(a, b, 1, 1)));
        edges.push((2 * i + 1, 2 * N + 1, dist(c, d, H, W)));
        edges.push((2 * i, 2 * i + 1, 1));
    }

    for i in 0..N {
        for j in 0..N {
            let (a1, b1, c1, d1) = warp_points[i];
            let (a2, b2, c2, d2) = warp_points[j];

            edges.push((2 * i + 1, 2 * j, dist(c1, d1, a2, b2)));
        }
    }

    let dist = dijkstras_algorithm(2 * N + 2, &edges, 2 * N)[2 * N + 1].unwrap();
    writeln!(out, "{}", dist);
}

pub fn dijkstras_algorithm<
    W: Sized + std::ops::Add<Output = W> + PartialOrd + Ord + Default + Clone + Copy,
>(
    size: usize,
    edges: &[(usize, usize, W)],
    start: usize,
) -> Vec<Option<W>> {
    assert!(start < size);

    let graph = {
        let mut g = vec![vec![]; size];
        for &(from, to, weight) in edges.iter() {
            assert!(from < size && to < size);
            g[from].push((to, weight));
        }
        g
    };

    let mut dist = vec![None; size];
    dist[start] = Some(W::default());

    let mut hq = std::collections::BinaryHeap::new();

    hq.push((std::cmp::Reverse(W::default()), start));

    while let Some((_, now)) = hq.pop() {
        for &(nxt, weight) in graph[now].iter() {
            match dist[nxt] {
                Some(prev_dist) => {
                    if dist[now].unwrap() + weight < prev_dist {
                        let d = dist[now].unwrap() + weight;
                        dist[nxt] = Some(d);
                        hq.push((std::cmp::Reverse(d), nxt));
                    }
                }
                None => {
                    let d = dist[now].unwrap() + weight;
                    dist[nxt] = Some(d);
                    hq.push((std::cmp::Reverse(d), nxt));
                }
            }
        }
    }

    dist
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0