結果
問題 | No.1324 Approximate the Matrix |
ユーザー | akakimidori |
提出日時 | 2020-12-21 04:43:35 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 66 ms / 2,000 ms |
コード長 | 5,245 bytes |
コンパイル時間 | 24,092 ms |
コンパイル使用メモリ | 387,500 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-21 12:39:57 |
合計ジャッジ時間 | 17,228 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 37 ms
6,940 KB |
testcase_04 | AC | 34 ms
6,940 KB |
testcase_05 | AC | 36 ms
6,940 KB |
testcase_06 | AC | 36 ms
6,944 KB |
testcase_07 | AC | 35 ms
6,948 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 3 ms
6,944 KB |
testcase_10 | AC | 5 ms
6,940 KB |
testcase_11 | AC | 8 ms
6,944 KB |
testcase_12 | AC | 3 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,944 KB |
testcase_14 | AC | 9 ms
6,940 KB |
testcase_15 | AC | 3 ms
6,940 KB |
testcase_16 | AC | 1 ms
6,940 KB |
testcase_17 | AC | 9 ms
6,940 KB |
testcase_18 | AC | 2 ms
6,944 KB |
testcase_19 | AC | 3 ms
6,940 KB |
testcase_20 | AC | 2 ms
6,940 KB |
testcase_21 | AC | 2 ms
6,940 KB |
testcase_22 | AC | 2 ms
6,944 KB |
testcase_23 | AC | 9 ms
6,940 KB |
testcase_24 | AC | 13 ms
6,944 KB |
testcase_25 | AC | 9 ms
6,944 KB |
testcase_26 | AC | 8 ms
6,944 KB |
testcase_27 | AC | 5 ms
6,940 KB |
testcase_28 | AC | 1 ms
6,940 KB |
testcase_29 | AC | 1 ms
6,944 KB |
testcase_30 | AC | 1 ms
6,944 KB |
testcase_31 | AC | 1 ms
6,944 KB |
testcase_32 | AC | 1 ms
6,940 KB |
testcase_33 | AC | 1 ms
6,940 KB |
testcase_34 | AC | 1 ms
6,944 KB |
testcase_35 | AC | 1 ms
6,944 KB |
testcase_36 | AC | 1 ms
6,944 KB |
testcase_37 | AC | 66 ms
6,940 KB |
testcase_38 | AC | 65 ms
6,944 KB |
testcase_39 | AC | 64 ms
6,940 KB |
testcase_40 | AC | 65 ms
6,940 KB |
testcase_41 | AC | 66 ms
6,944 KB |
testcase_42 | AC | 3 ms
6,940 KB |
testcase_43 | AC | 3 ms
6,944 KB |
testcase_44 | AC | 2 ms
6,944 KB |
ソースコード
// 最短路反復法 // src -> dst へflowだけ流せた時コストを返す // 負閉路はないと仮定 const INF: i64 = 1_000_000_000_000_000i64 + 1; struct Graph { size: usize, edge: Vec<(usize, usize, i64, i64)>, } impl Graph { fn new(size: usize) -> Self { Graph { size: size, edge: vec![], } } fn add_edge(&mut self, src: usize, dst: usize, capa: i64, cost: i64) { assert!(src < self.size && dst < self.size && src != dst); self.edge.push((src, dst, capa, cost)); } fn flow(&self, src: usize, dst: usize, flow: i64) -> Result<i64, (i64, i64)> { if src == dst { return Ok(0); } let size = self.size; let edge = &self.edge; let mut deg = vec![0; size]; for &(a, b, _, _) in edge.iter() { deg[a] += 1; deg[b] += 1; } let mut graph: Vec<_> = deg.into_iter().map(|d| Vec::with_capacity(d)).collect(); for &(a, b, capa, cost) in edge.iter() { let x = graph[a].len(); let y = graph[b].len(); graph[a].push((b, capa, cost, y)); graph[b].push((a, 0, -cost, x)); } let mut ans = 0; let mut rem = flow; let mut dp = Vec::with_capacity(size); let mut pot = vec![0; size]; let mut h = std::collections::BinaryHeap::new(); while rem > 0 { dp.clear(); dp.resize(size, (INF, src, 0));// コスト、親、親からの番号 dp[src] = (0, src, 0); h.push((0, src)); while let Some((d, v)) = h.pop() { let d = -d; if d > dp[v].0 { continue; } for (i, &(u, capa, cost, _)) in graph[v].iter().enumerate() { if capa == 0 { continue; } let c = d + cost - pot[u] + pot[v]; if c < dp[u].0 { dp[u] = (c, v, i); h.push((-c, u)); } } } if dp[dst].0 == INF { return Err((flow - rem, ans)); } for i in 0..size { pot[i] -= dp[dst].0 - dp[i].0; } let mut sub = rem; let mut pos = dst; while pos != src { let (_, parent, k) = dp[pos]; sub = sub.min(graph[parent][k].1); pos = parent; } let mut pos = dst; while pos != src { let (_, parent, k) = dp[pos]; let inv = graph[parent][k].3; graph[parent][k].1 -= sub; graph[pos][inv].1 += sub; pos = parent; } rem -= sub; ans += -pot[src] * sub; } Ok(ans) } } // ---------- 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! { n: usize, k: i64, a: [i64; n], b: [i64; n], p: [[i64; n]; n], } let geta = 500; let mut g = Graph::new(2 * n + 2); let src = 2 * n + 1; let dst = src - 1; for (i, a) in a.iter().enumerate() { g.add_edge(src, i, *a, 0); } for (i, b) in b.iter().enumerate() { g.add_edge(i + n, dst, *b, 0); } for (i, p) in p.iter().enumerate() { for (j, &p) in p.iter().enumerate() { let sup = a[i].min(b[j]); for v in 0..sup { let diff = (p - v - 1).pow(2) - (p - v).pow(2); g.add_edge(i, j + n, 1, diff + geta); } } } let ans = p.iter().flatten().fold(0, |s, a| s + *a * *a) + g.flow(src, dst, k).unwrap() - geta * k; println!("{}", ans); } fn main() { run(); }