結果
問題 | No.957 植林 |
ユーザー | akakimidori |
提出日時 | 2021-01-19 17:30:35 |
言語 | Rust (1.77.0 + proconio) |
結果 |
TLE
|
実行時間 | - |
コード長 | 7,467 bytes |
コンパイル時間 | 13,771 ms |
コンパイル使用メモリ | 393,188 KB |
実行使用メモリ | 39,552 KB |
最終ジャッジ日時 | 2024-12-17 13:51:16 |
合計ジャッジ時間 | 86,488 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
13,636 KB |
testcase_01 | AC | 2 ms
24,356 KB |
testcase_02 | AC | 2 ms
13,640 KB |
testcase_03 | AC | 89 ms
35,200 KB |
testcase_04 | AC | 95 ms
23,200 KB |
testcase_05 | AC | 80 ms
24,996 KB |
testcase_06 | AC | 76 ms
37,504 KB |
testcase_07 | AC | 70 ms
23,844 KB |
testcase_08 | AC | 42 ms
24,484 KB |
testcase_09 | AC | 41 ms
36,864 KB |
testcase_10 | AC | 69 ms
25,248 KB |
testcase_11 | AC | 44 ms
37,248 KB |
testcase_12 | AC | 45 ms
24,480 KB |
testcase_13 | AC | 31 ms
21,924 KB |
testcase_14 | AC | 44 ms
39,552 KB |
testcase_15 | AC | 38 ms
24,224 KB |
testcase_16 | AC | 33 ms
22,052 KB |
testcase_17 | AC | 34 ms
23,584 KB |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
testcase_22 | TLE | - |
testcase_23 | TLE | - |
testcase_24 | TLE | - |
testcase_25 | TLE | - |
testcase_26 | TLE | - |
testcase_27 | TLE | - |
testcase_28 | TLE | - |
testcase_29 | TLE | - |
testcase_30 | TLE | - |
testcase_31 | TLE | - |
testcase_32 | TLE | - |
testcase_33 | TLE | - |
testcase_34 | TLE | - |
testcase_35 | TLE | - |
testcase_36 | TLE | - |
testcase_37 | TLE | - |
testcase_38 | TLE | - |
testcase_39 | TLE | - |
testcase_40 | TLE | - |
testcase_41 | AC | 25 ms
18,560 KB |
testcase_42 | AC | 27 ms
19,352 KB |
testcase_43 | AC | 30 ms
19,328 KB |
testcase_44 | AC | 44 ms
20,736 KB |
testcase_45 | AC | 1 ms
6,816 KB |
testcase_46 | AC | 1 ms
6,816 KB |
testcase_47 | AC | 1 ms
33,868 KB |
ソースコード
// ---------- begin max flow (Dinic) ---------- mod maxflow { pub trait MaxFlowCapacity: Copy + Ord + std::ops::Add<Output = Self> + std::ops::Sub<Output = Self> { fn zero() -> Self; fn inf() -> Self; } macro_rules! impl_primitive_integer_capacity { ($x:ty) => { impl MaxFlowCapacity for $x { fn zero() -> Self { 0 } fn inf() -> Self { <$x>::MAX } } }; } impl_primitive_integer_capacity!(u32); impl_primitive_integer_capacity!(u64); impl_primitive_integer_capacity!(i32); impl_primitive_integer_capacity!(i64); #[derive(Clone, Copy)] struct Edge<Cap> { to_: u32, inv_: u32, cap_: Cap, } impl<Cap> Edge<Cap> { fn new(to: usize, inv: usize, cap: Cap) -> Self { Edge { to_: to as u32, inv_: inv as u32, cap_: cap, } } fn to(&self) -> usize { self.to_ as usize } fn inv(&self) -> usize { self.inv_ as usize } } impl<Cap: MaxFlowCapacity> Edge<Cap> { fn add(&mut self, cap: Cap) { self.cap_ = self.cap_ + cap; } fn sub(&mut self, cap: Cap) { self.cap_ = self.cap_ - cap; } fn cap(&self) -> Cap { self.cap_ } } pub struct Graph<Cap> { graph: Vec<Vec<Edge<Cap>>>, } #[allow(dead_code)] pub struct EdgeIndex { src: usize, dst: usize, x: usize, y: usize, } impl<Cap: MaxFlowCapacity> Graph<Cap> { pub fn new(size: usize) -> Self { Self { graph: vec![vec![]; size], } } pub fn add_edge(&mut self, src: usize, dst: usize, cap: Cap) -> EdgeIndex { assert!(src.max(dst) < self.graph.len()); assert!(cap >= Cap::zero()); assert!(src != dst); let x = self.graph[src].len(); let y = self.graph[dst].len(); self.graph[src].push(Edge::new(dst, y, cap)); self.graph[dst].push(Edge::new(src, x, Cap::zero())); EdgeIndex { src, dst, x, y } } // src, dst, used, intial_capacity #[allow(dead_code)] pub fn get_edge(&self, e: &EdgeIndex) -> (usize, usize, Cap, Cap) { let max = self.graph[e.src][e.x].cap() + self.graph[e.dst][e.y].cap(); let used = self.graph[e.dst][e.y].cap(); (e.src, e.dst, used, max) } pub fn flow(&mut self, src: usize, dst: usize) -> Cap { let size = self.graph.len(); assert!(src.max(dst) < size); assert!(src != dst); let mut queue = std::collections::VecDeque::new(); let mut level = vec![0; size]; let mut it = vec![0; size]; let mut ans = Cap::zero(); loop { queue.clear(); level.clear(); level.resize(size, 0); level[src] = 1; queue.push_back(src); while let Some(v) = queue.pop_front() { if v == dst { break; } let d = level[v] + 1; for e in self.graph[v].iter() { let u = e.to(); if e.cap() > Cap::zero() && level[u] == 0 { level[u] = d; queue.push_back(u); } } } if level[dst] == 0 { break; } it.clear(); it.resize(size, 0); loop { let f = self.dfs(dst, src, Cap::inf(), &mut it, &level); if f == Cap::zero() { break; } ans = ans + f; } } ans } fn dfs(&mut self, v: usize, src: usize, cap: Cap, it: &mut [usize], level: &[u32]) -> Cap { if v == src { return cap; } while it[v] < self.graph[v].len() { let (u, inv) = { let po = &self.graph[v][it[v]]; (po.to(), po.inv()) }; if level[u] + 1 == level[v] && self.graph[u][inv].cap() > Cap::zero() { let cap = std::cmp::min(cap, self.graph[u][inv].cap()); let c = self.dfs(u, src, cap, it, level); if c > Cap::zero() { self.graph[v][it[v]].add(c); self.graph[u][inv].sub(c); return c; } } it[v] += 1; } Cap::zero() } } } // ---------- end max flow (Dinic) ---------- // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 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_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_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 ---------- fn run() { input! { h: usize, w: usize, g: [[i64; w]; h], r: [i64; h], c: [i64; w], } let mut graph = maxflow::Graph::new(h * w + h + w + 2); let src = h * w + h + w; let dst = src + 1; let sum = r.iter().chain(c.iter()).sum::<i64>(); for (i, g) in g.iter().enumerate() { for (j, g) in g.iter().enumerate() { graph.add_edge(i * w + j, dst, *g); } } let inf = 10i64.pow(9) * 300 + 1; for (i, r) in r.iter().enumerate() { graph.add_edge(src, h * w + i, *r); for j in 0..w { graph.add_edge(h * w + i, i * w + j, inf); } } for (j, c) in c.iter().enumerate() { graph.add_edge(src, h * w + h + j, *c); for i in 0..h { graph.add_edge(h * w + h + j, i * w + j, inf); } } let ans = sum - graph.flow(src, dst); println!("{}", ans); } fn main() { run(); }