// from https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[allow(unused_macros)] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let mut 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)*} }; } #[allow(unused_macros)] 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)*} }; } #[allow(unused_macros)] 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") }; } #[allow(unused_macros)] macro_rules! debug { ($x: expr) => { println!("{}: {:?}", stringify!($x), $x) }; } // macro_rules! debug { ($x: expr) => () } #[allow(dead_code)] fn show(iter: T) -> String where T: Iterator, T::Item: std::fmt::Display, { let mut res = iter.fold(String::new(), |sum, e| sum + &format!("{} ", e)); res.pop(); res } #[allow(unused_imports)] use std::cmp::{max, min}; #[allow(unused_imports)] use std::collections::btree_map::Entry::{Occupied, Vacant}; #[allow(unused_imports)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; #[allow(unused_imports)] use std::mem::swap; use std::clone::Clone; use std::cmp::Ordering; #[derive(Clone, Debug, Hash, Eq, PartialEq)] struct Edge where Cost: std::clone::Clone, { to: usize, cost: Cost, } impl Ord for Edge where Cost: Ord + std::clone::Clone, { fn cmp(&self, other: &Edge) -> Ordering { (self.cost).cmp(&(other.cost)).reverse() } } impl PartialOrd for Edge where Cost: std::clone::Clone, Edge: Ord, { fn partial_cmp(&self, other: &Edge) -> Option { Some(self.cmp(other)) } } trait Graph where Self::Cost: Clone, { type Cost; fn len(&self) -> usize; fn get<'a>(&'a self, p: usize) -> std::slice::Iter<'a, Edge>; fn get_order(&self) -> Vec; } #[derive(Clone, Debug, Hash)] struct AdjList { adj: Vec>>, } impl std::ops::Index for AdjList { type Output = Vec>; fn index(&self, i: usize) -> &Vec> { &self.adj[i] } } impl std::ops::IndexMut for AdjList { fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut Vec> { &mut self.adj[i] } } impl AdjList { fn new(n: usize) -> AdjList { AdjList { adj: vec![vec![]; n], } } fn add_edge(&mut self, from: usize, to_: usize, cost_: Cost) { self.adj[from].push(Edge { to: to_, cost: cost_, }); } fn add_uedge(&mut self, from: usize, to_: usize, cost_: Cost) { self.add_edge(from, to_, cost_.clone()); self.add_edge(to_, from, cost_); } } impl Graph for AdjList where Cost: Clone, { type Cost = Cost; fn len(&self) -> usize { self.adj.len() } fn get<'a>(&'a self, p: usize) -> std::slice::Iter<'a, Edge> { self.adj[p].iter() } fn get_order(&self) -> Vec { let mut res = Vec::with_capacity(self.len()); let mut deg = vec![0; self.len()]; let mut used = vec![false; self.len()]; for from in 0..self.len() { for e in self.get(from) { deg[e.to] += 1; } } for s in 0..self.len() { if deg[s] != 0 || used[s] { continue; } let mut stack = Vec::new(); used[s] = true; stack.push(s); while let Some(p) = stack.pop() { used[p] = true; res.push(p); for e in self.get(p) { deg[e.to] -= 1; if deg[e.to] == 0 { stack.push(e.to); } } } } // debug!(deg); res } } // -------------------- trait Zero { fn zero() -> Self; } impl Zero for i64 { fn zero() -> i64 { 0 } } impl Zero for i32 { fn zero() -> i32 { 0 } } trait Max { fn max_value() -> Self; } impl Max for i64 { fn max_value() -> i64 { std::i64::MAX } } impl Max for i32 { fn max_value() -> i32 { std::i32::MAX } } trait GraphHavingOrderedCost: Graph where Self::Cost: Ord + Add + Zero + Max + Copy, { fn get_shortest_path(&self, from: usize) -> (Vec, ()); fn bellmanford(&self, from: usize) -> (Vec, Vec); fn get_shortest_path_graph(&self, from: usize, dist: &Vec) -> AdjList; } use std::ops::Add; impl GraphHavingOrderedCost for T where T: Graph, T::Cost: Ord + Add + Zero + Max + Copy, Edge: Ord, { fn get_shortest_path(&self, from: usize) -> (Vec, ()) { let n = self.len(); let mut dist = vec![Self::Cost::max_value(); n]; // let mut pre = vec![n as usize; n]; let mut heap = std::collections::BinaryHeap::new(); heap.push(Edge { to: from, cost: Self::Cost::zero(), }); dist[from] = Self::Cost::zero(); while let Some(p) = heap.pop() { for e in self.get(p.to) { if p.cost + e.cost >= dist[e.to] { continue; } heap.push(Edge { to: e.to, cost: p.cost + e.cost, }); dist[e.to] = p.cost + e.cost; // pre[e.to] = p.to; } } (dist, ()) } fn bellmanford(&self, from: usize) -> (Vec, Vec) { let n = self.len(); let mut dist = vec![Self::Cost::max_value(); n]; let mut updated = vec![true; n]; dist[from] = Self::Cost::zero(); for _ in 0..n + 1 { updated = vec![false; n]; for from in 0..n { if dist[from] == Self::Cost::max_value() { continue; } for e in self.get(from) { if dist[e.to] > dist[from] + e.cost { dist[e.to] = dist[from] + e.cost; updated[e.to] = true; } } } } (dist, updated) } fn get_shortest_path_graph(&self, from: usize, dist: &Vec) -> AdjList { let n = self.len(); let mut res = AdjList::new(n); for from in 0..n { for e in self.get(from) { if dist[from] + e.cost == dist[e.to] { res.add_edge(from, e.to, e.cost); } } } res } } impl AdjList { fn vizualize(&self, name: &str) { let path = std::path::Path::new("tmp.dot"); { use std::io::{BufWriter, Write}; let file = std::fs::File::create(path).unwrap(); let mut out = BufWriter::new(file); let n = self.len(); writeln!(&mut out, "digraph graph_name {{").unwrap(); for p in 0..n { writeln!(&mut out, " {}[];", p); } for p in 0..n { for e in &self[p] { writeln!(&mut out, " {} -> {}", p, e.to); } } writeln!(&mut out, "}}"); } let mut cmd = std::process::Command::new("dot"); cmd.arg("-T"); cmd.arg("png"); cmd.arg("tmp.dot"); cmd.arg("-o"); cmd.arg(name); cmd.status().expect("failed to execute dot"); } } impl AdjList { fn viz_with_cost(&self, name: &str) { let path = std::path::Path::new("tmp.dot"); { use std::io::{BufWriter, Write}; let file = std::fs::File::create(path).unwrap(); let mut out = BufWriter::new(file); let n = self.len(); writeln!(&mut out, "digraph graph_name {{").unwrap(); for p in 0..n { writeln!(&mut out, " {}[];", p); } for p in 0..n { for e in &self[p] { writeln!(&mut out, " {} -> {} [label={}]", p, e.to, e.cost); } } writeln!(&mut out, "}}"); } let mut cmd = std::process::Command::new("dot"); cmd.arg("-T"); cmd.arg("png"); cmd.arg("tmp.dot"); cmd.arg("-o"); cmd.arg(name); cmd.status().expect("failed to execute dot"); } } fn main() { use std::io::Write; let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); #[allow(unused_macros)] macro_rules! printf { ($($arg:tt)*) => (write!(out, $($arg)*).unwrap()); } #[allow(unused_macros)] macro_rules! printfln { () => (writeln!(out).unwrap()); ($($arg:tt)*) => (writeln!(out, $($arg)*).unwrap()); } input! { n: usize, v: i32, ox: usize, oy: usize, l: [[i32; n]; n], } // let v = v - l[0][0]; let dx = [0, -1, 0, 1]; let dy = [-1, 0, 1, 0]; let mut g = AdjList::new(n * n); let get_idx = |i, j| i * n + j; for i in 0..n { for j in 0..n { for k in 0..4 { let ni = i as i64 + dx[k]; let nj = j as i64 + dy[k]; if 0 <= ni && ni < n as i64 && 0 <= nj && nj < n as i64 { let ni = ni as usize; let nj = nj as usize; g.add_edge(get_idx(i, j), get_idx(ni, nj), l[ni][nj]); } } } } let mut ans = false; let (dist0, _) = g.get_shortest_path(get_idx(0, 0)); // debug!(v - dist0[get_idx(n - 1, n - 1)]); if v - dist0[get_idx(n - 1, n - 1)] > 0 { ans = true; } if ox != 0 { let ox = ox - 1; let oy = oy - 1; let (dist1, _) = g.get_shortest_path(get_idx(oy, ox)); // debug!((v - dist0[get_idx(ox, oy)]) * 2 - dist1[get_idx(n - 1, n - 1)]); if (v - dist0[get_idx(oy, ox)]) * 2 - dist1[get_idx(n - 1, n - 1)] > 0 { ans = true; } } if ans { printfln!("YES"); } else { printfln!("NO"); } // fn dfs(x: usize, y: usize, ) }