結果

問題 No.2695 Warp Zone
ユーザー nautnaut
提出日時 2024-03-22 22:21:49
言語 Rust
(1.77.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 3,707 bytes
コンパイル時間 2,575 ms
コンパイル使用メモリ 191,936 KB
実行使用メモリ 41,780 KB
最終ジャッジ日時 2024-03-22 22:22:24
合計ジャッジ時間 2,339 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 36 ms
41,780 KB
testcase_04 AC 35 ms
41,396 KB
testcase_05 AC 36 ms
41,396 KB
testcase_06 AC 36 ms
41,652 KB
testcase_07 AC 36 ms
41,780 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 16 ms
18,088 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 6 ms
6,676 KB
testcase_12 AC 21 ms
23,340 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 26 ms
29,872 KB
testcase_15 AC 14 ms
15,104 KB
testcase_16 AC 5 ms
6,676 KB
testcase_17 AC 9 ms
10,432 KB
testcase_18 AC 31 ms
35,508 KB
testcase_19 AC 1 ms
6,676 KB
testcase_20 AC 28 ms
32,564 KB
testcase_21 AC 26 ms
29,744 KB
testcase_22 AC 11 ms
11,840 KB
testcase_23 AC 21 ms
23,340 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `a1`
  --> 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`
  --> 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`
  --> 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`
  --> Main.rs:49:30
   |
49 |             let (a2, b2, c2, d2) = warp_points[j];
   |                              ^^ help: if this is intentional, prefix it with an underscore: `_d2`

warning: 4 warnings emitted

ソースコード

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