結果
問題 | No.1288 yuki collection |
ユーザー | akakimidori |
提出日時 | 2020-11-29 12:27:01 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 142 ms / 5,000 ms |
コード長 | 5,378 bytes |
コンパイル時間 | 13,460 ms |
コンパイル使用メモリ | 396,832 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-13 01:57:17 |
合計ジャッジ時間 | 16,613 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,944 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,944 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 136 ms
6,940 KB |
testcase_14 | AC | 130 ms
6,940 KB |
testcase_15 | AC | 106 ms
6,940 KB |
testcase_16 | AC | 109 ms
6,940 KB |
testcase_17 | AC | 129 ms
6,944 KB |
testcase_18 | AC | 138 ms
6,944 KB |
testcase_19 | AC | 141 ms
6,944 KB |
testcase_20 | AC | 135 ms
6,940 KB |
testcase_21 | AC | 101 ms
6,944 KB |
testcase_22 | AC | 103 ms
6,940 KB |
testcase_23 | AC | 101 ms
6,940 KB |
testcase_24 | AC | 142 ms
6,944 KB |
testcase_25 | AC | 139 ms
6,940 KB |
testcase_26 | AC | 136 ms
6,944 KB |
testcase_27 | AC | 55 ms
6,944 KB |
testcase_28 | AC | 71 ms
6,944 KB |
testcase_29 | AC | 81 ms
6,944 KB |
testcase_30 | AC | 4 ms
6,944 KB |
testcase_31 | AC | 6 ms
6,944 KB |
testcase_32 | AC | 6 ms
6,940 KB |
testcase_33 | AC | 87 ms
6,944 KB |
testcase_34 | AC | 77 ms
6,940 KB |
testcase_35 | AC | 73 ms
6,944 KB |
testcase_36 | AC | 87 ms
6,944 KB |
testcase_37 | AC | 87 ms
6,944 KB |
testcase_38 | AC | 25 ms
6,940 KB |
testcase_39 | AC | 28 ms
6,940 KB |
testcase_40 | AC | 1 ms
6,940 KB |
testcase_41 | AC | 1 ms
6,940 KB |
testcase_42 | AC | 1 ms
6,944 KB |
ソースコード
// 最短路反復法 // src -> dst へflowだけ流せた時コストを返す // 負閉路はないと仮定 const INF: i64 = 1_000_000_000_000_000i64 + 1; const GETA: i64 = 1_000_000_000; 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)); } 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; } rem -= sub; ans = ans.max(ans + GETA * 4 * sub - dp[dst].0 - pot[dst]); 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; } for i in 0..size { pot[i] -= pot[dst] - pot[i]; } } 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, s: bytes, v: [i64; n], } assert!(s.len() == n); let mut g = Graph::new(n + 2); let src = n; let dst = n + 1; let mut prev: Vec<Option<usize>> = vec![None; 4]; for (i, (&c, &v)) in s.iter().zip(v.iter()).enumerate().rev() { let k = match c { b'y' => 0, b'u' => 1, b'k' => 2, b'i' => 3, _ => unreachable!(), }; if k == 3 { g.add_edge(i, dst, 1, GETA - v); } else if let Some(p) = prev[k + 1] { g.add_edge(i, p, 1, GETA - v); } if let Some(p) = prev[k] { g.add_edge(i, p, 2000, 0); } prev[k] = Some(i); } if let Some(v) = prev[0] { g.add_edge(src, v, 2000, 0); } let ans = g.flow(src, dst, 501).unwrap_err().1; println!("{}", ans); } fn main() { run(); }