#![allow(non_snake_case)] #![allow(unused_imports)] // use itertools::Itertools; // use proconio::input; // use proconio::marker::*; use std::cmp::Reverse; use std::collections::*; fn main() { input! { n:usize, v:usize, ox:usize, oy:usize, l:[[usize;n];n], } let ans0 = dijkstra(&l, 0, 0, n - 1, n - 1) < v; let ans1 = if ox == 0 { false } else { let dist = dijkstra(&l, 0, 0, ox - 1, oy - 1); let hp = if dist < v { (v - dist) * 2 } else { 0 }; hp > dijkstra(&l, n - 1, n - 1, ox - 1, oy - 1) - l[ox - 1][oy - 1] }; let ans = ans0 || ans1; println!("{}", if ans { "YES" } else { "NO" }); } fn dijkstra(l: &Vec>, sx: usize, sy: usize, gx: usize, gy: usize) -> usize { let n = l.len(); let gmgr = GridMgr::new(n, n); const INF: usize = std::usize::MAX; let mut dists = vec![vec![INF; n]; n]; let mut pq = BinaryHeap::new(); dists[sx][sy] = 0; pq.push(Reverse((dists[sx][sy], sx, sy))); while let Some(Reverse((dist, cx, cy))) = pq.pop() { if dists[cx][cy] < dist { continue; } for (nx, ny) in gmgr.neigh4((cx, cy)) { let ndist = dist + l[nx][ny]; if ndist < dists[nx][ny] { dists[nx][ny] = ndist; pq.push(Reverse((ndist, nx, ny))); } } } dists[gx][gy] } use grid_mgr::*; mod grid_mgr { pub const DIR4: [(usize, usize); 4] = [(1, 0), (0, !0), (!0, 0), (0, 1)]; pub const DIR8: [(usize, usize); 8] = [ (1, 0), (1, !0), (0, !0), (!0, !0), (!0, 0), (!0, 1), (0, 1), (1, 1), ]; pub struct GridMgr { pub h: usize, pub w: usize, } impl GridMgr { #[allow(dead_code)] pub fn new(h: usize, w: usize) -> Self { Self { h, w } } #[allow(dead_code)] pub fn neigh4<'a>( &'a self, p: (usize, usize), ) -> impl Iterator + 'a { DIR4.iter().filter_map(move |&(dx, dy)| { let nx = p.0.wrapping_add(dx); let ny = p.1.wrapping_add(dy); if nx < self.h && ny < self.w { Some((nx, ny)) } else { None } }) } #[allow(dead_code)] pub fn neigh8<'a>( &'a self, p: (usize, usize), ) -> impl Iterator + 'a { DIR8.iter().filter_map(move |&(dx, dy)| { let nx = p.0.wrapping_add(dx); let ny = p.1.wrapping_add(dy); if nx < self.h && ny < self.w { Some((nx, ny)) } else { None } }) } } } mod input { #[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::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } }