結果

問題 No.2366 登校
ユーザー akakimidoriakakimidori
提出日時 2023-06-30 21:53:02
言語 Rust
(1.77.0)
結果
AC  
実行時間 270 ms / 4,000 ms
コード長 3,709 bytes
コンパイル時間 1,230 ms
コンパイル使用メモリ 162,100 KB
実行使用メモリ 18,512 KB
最終ジャッジ日時 2023-09-03 02:59:31
合計ジャッジ時間 3,882 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 82 ms
6,232 KB
testcase_13 AC 75 ms
6,168 KB
testcase_14 AC 76 ms
6,204 KB
testcase_15 AC 85 ms
6,212 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 84 ms
6,232 KB
testcase_18 AC 75 ms
6,240 KB
testcase_19 AC 83 ms
6,232 KB
testcase_20 AC 82 ms
6,232 KB
testcase_21 AC 74 ms
6,208 KB
testcase_22 AC 62 ms
6,104 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 57 ms
18,416 KB
testcase_25 AC 270 ms
18,512 KB
testcase_26 AC 264 ms
18,484 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::io::Write`
 --> Main.rs:1:5
  |
1 | use std::io::Write;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: type alias `Map` is never used
 --> Main.rs:4:6
  |
4 | type Map<K, V> = BTreeMap<K, V>;
  |      ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
 --> Main.rs:5:6
  |
5 | type Set<T> = BTreeSet<T>;
  |      ^^^

warning: type alias `Deque` is never used
 --> Main.rs:6:6
  |
6 | type Deque<T> = VecDeque<T>;
  |      ^^^^^

warning: 4 warnings emitted

ソースコード

diff #

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn main() {
    input! {
        h: usize,
        w: usize,
        k: usize,
        t: usize,
        p: [(usize1, usize1, usize, i64); k],
    }
    if t >= h - 1 + w - 1 {
        println!("0");
        return;
    }
    let mut state = vec![vec![None; w]; h];
    for (x, y, c, d) in p {
        state[x][y] = Some((c, d));
    }
    let g = h - 1 + w - 1;
    let inf = std::i64::MAX / 2;
    let mut dp = vec![vec![vec![inf; 2 * g + 1]; w]; h];
    dp[0][0][g] = 0;
    let mut pq = std::collections::BinaryHeap::new();
    pq.push((0, 0, 0, g));
    let mut ans = inf;
    while let Some((d, x, y, k)) = pq.pop() {
        let d = -d;
        if d > dp[x][y][k] {
            continue;
        }
        for &(dx, dy) in [(1, 0), (0, 1), (!0, 0), (0, !0)].iter() {
            let x = x + dx;
            let y = y + dy;
            if x < h && y < w && k + 1 <= 2 * g && dp[x][y][k + 1].chmin(d) {
                pq.push((-d, x, y, k + 1));
            }
        }
        if let Some((a, b)) = state[x][y] {
            if k >= a - 1 {
                if dp[x][y][k - (a - 1)].chmin(d + b) {
                    pq.push((-(d + b), x, y, k - (a - 1)));
                }
            } else {
                ans.chmin(d + b);
            }
        }
    }
    for (x, dp) in dp.iter().enumerate() {
        for (y, dp) in dp.iter().enumerate() {
            for (k, dp) in dp.iter().enumerate() {
                if k + (h - 1 - x) + (w - 1 - y) <= t + g {
                    ans.chmin(*dp);
                }
            }
        }
    }
    if ans == inf {
        ans = -1;
    }
    println!("{}", ans);
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------
// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------

0