結果

問題 No.2695 Warp Zone
ユーザー naut3naut3
提出日時 2024-03-22 22:18:30
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 3,707 bytes
コンパイル時間 12,427 ms
コンパイル使用メモリ 378,748 KB
実行使用メモリ 41,856 KB
最終ジャッジ日時 2024-09-30 11:46:40
合計ジャッジ時間 14,048 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 1 ms
5,248 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 13 ms
16,640 KB
testcase_10 AC 1 ms
5,248 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
5,248 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 1 ms
5,248 KB
testcase_25 AC 1 ms
5,248 KB
testcase_26 AC 1 ms
5,248 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `c1`
  --> src/main.rs:48:26
   |
48 |             let (a1, b1, c1, d1) = warp_points[i];
   |                          ^^ help: if this is intentional, prefix it with an underscore: `_c1`
   |
   = note: `#[warn(unused_variables)]` on by default

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

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

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

ソースコード

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, 2 * j + 1, dist(a1, b1, c2, d2)));
        }
    }

    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