結果
問題 | No.1479 Matrix Eraser |
ユーザー | fukafukatani |
提出日時 | 2021-04-17 00:47:23 |
言語 | Rust (1.77.0 + proconio) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,105 bytes |
コンパイル時間 | 12,210 ms |
コンパイル使用メモリ | 401,880 KB |
実行使用メモリ | 19,100 KB |
最終ジャッジ日時 | 2024-07-03 06:15:20 |
合計ジャッジ時間 | 21,421 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
19,100 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 981 ms
5,376 KB |
testcase_08 | AC | 1,757 ms
7,868 KB |
testcase_09 | TLE | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
ソースコード
#![allow(unused_imports)] use std::cmp::*; use std::collections::*; use std::io::Write; use std::ops::Bound::*; #[allow(unused_macros)] macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; } fn main() { let v = read_vec::<usize>(); let (h, w) = (v[0], v[1]); let mut poses = HashMap::new(); for y in 0..h { let row = read_vec::<i64>(); for x in 0..w { if row[x] == 0 { continue; } poses.entry(row[x]).or_insert(vec![]).push((x, y)); } } let mut ans = 0; for ref v in poses.values() { let s = h + w; let t = h + w + 1; let mut flow = Solver::new(h + w + 2); for y in 0..h { flow.add_edge(s, y, 1); } for x in 0..w { flow.add_edge(h + x, t, 1); } for &(x, y) in *v { flow.add_edge(y, h + x, 1); } let m = flow.max_flow(s, t); // debug!(m, v); ans += m; } println!("{}", ans); } fn read<T: std::str::FromStr>() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } #[derive(Clone, Copy)] struct Edge { to: usize, cap: i64, rev: usize, } #[derive(Clone)] struct Solver { edges: Vec<Vec<Edge>>, used: Vec<bool>, } impl Solver { fn new(v: usize) -> Solver { Solver { edges: vec![Vec::new(); v], used: vec![false; v], } } fn add_edge(&mut self, from: usize, to: usize, cap: i64) { let rev = self.edges[to].len(); self.edges[from].push(Edge { to: to, cap: cap, rev: rev, }); let rev = self.edges[from].len() - 1; self.edges[to].push(Edge { to: from, cap: 0, rev: rev, }); } fn dfs(&mut self, v: usize, t: usize, f: i64) -> i64 { if v == t { return f; } self.used[v] = true; for i in 0..self.edges[v].len() { let edge = self.edges[v][i]; if !self.used[edge.to] && edge.cap > 0 { let d = self.dfs(edge.to, t, std::cmp::min(f, edge.cap)); if d > 0 { self.edges[v][i].cap -= d; self.edges[edge.to][edge.rev].cap += d; return d; } } } 0 } fn max_flow(&mut self, s: usize, t: usize) -> i64 { let mut flow = 0; loop { self.used = vec![false; self.edges.len()]; let f = self.dfs(s, t, std::i64::MAX); if f == 0 { return flow; } flow += f; } } }