結果
| 問題 | 
                            No.943 取り調べ
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-01-25 10:27:44 | 
| 言語 | Rust  (1.83.0 + proconio)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 660 ms / 1,206 ms | 
| コード長 | 2,940 bytes | 
| コンパイル時間 | 15,554 ms | 
| コンパイル使用メモリ | 392,716 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-09-14 04:03:24 | 
| 合計ジャッジ時間 | 17,912 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 24 | 
ソースコード
use std::io::Read;
use std::collections::{HashMap, HashSet};
fn solve(n: usize, x: Vec<Vec<usize>>, a: Vec<usize>) {
    //0: 未, 1:済
    let mut dp: HashMap<String, usize> = HashMap::new();
    dp.insert(format!("{:0>1$b}", 0, n), 0);
    while !(dp.len() == 1 && dp.contains_key(&format!("{:0>1$b}", 2usize.pow(n as u32)-1, n))) {
        let old: HashMap<String, usize> = dp.clone().to_owned();
        dp.clear(); 
        for (used, min_cost) in old {
            used.chars().enumerate().for_each(|pair| {
                match pair.1 {
                    '0' => {
                        let mut cost: usize = 0;
                        let mut to_1: HashSet<usize> = HashSet::new();
                        for f2c in x[pair.0].iter().zip(used.chars()).enumerate() {
                            if (f2c.1).0 == &1 && (f2c.1).1 == '0' {
                                cost += a[f2c.0];
                                to_1.insert(f2c.0);
                            }
                        }
                        let next_str = used.clone().to_owned();
                        let next_str = next_str.chars().enumerate()
                            .map(|ic| {
                                if pair.0 == ic.0 || to_1.contains(&ic.0) { "1".to_string() } else { (ic.1).to_string() }
                            })
                            .collect::<Vec<String>>()
                            .join("");
                        if let Some(costs) = dp.get_mut(&next_str) {
                            *costs = std::cmp::min(*costs, min_cost + cost);
                        } else {
                            dp.insert(next_str, min_cost + cost);
                        }
                    },
                    _ => {
                        if used == format!("{:0>1$b}", 2usize.pow(n as u32)-1, n) {
                            let next_str = used.clone().to_owned();
                            if let Some(costs) = dp.get_mut(&next_str) {
                                *costs = std::cmp::min(*costs, min_cost);
                            } else {
                                dp.insert(next_str, min_cost);
                            }
                        } 
                    },
                }
            });
        }
    }
    println!("{}", dp.get(&format!("{:0>1$b}", 2usize.pow(n as u32)-1, n)).unwrap());
}
fn main() {
    let mut all_data = String::new();
    std::io::stdin().read_to_string(&mut all_data).ok();
    let all_data: Vec<&str> = all_data.trim().split('\n').map(|i| i.trim()).collect();
    let n: usize = all_data.iter().next().unwrap().parse::<usize>().unwrap();
    let x: Vec<Vec<usize>> = all_data.iter().skip(1).take(n).map(|i| i.split_whitespace().map(|j| j.parse::<usize>().unwrap()).collect()).collect();
    let a: Vec<usize> = all_data.iter().skip(n+1).next().unwrap().split_whitespace().map(|i| i.parse::<usize>().unwrap()).collect();
    solve(n, x, a);
}