結果

問題 No.943 取り調べ
ユーザー htensai
提出日時 2020-02-17 09:06:22
言語 Java
(openjdk 23)
結果
AC  
実行時間 102 ms / 1,206 ms
コード長 1,331 bytes
コンパイル時間 1,904 ms
コンパイル使用メモリ 77,820 KB
実行使用メモリ 39,156 KB
最終ジャッジ日時 2024-10-06 14:49:32
合計ジャッジ時間 4,271 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static int[] cost;
    static int[] trust;
    static int[] dp;
    static int n;
	public static void main (String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());
		trust = new int[n];
		for (int i = 0; i < n; i++) {
		    String[] line = br.readLine().split(" ", n);
		    for (int j = 0; j < n; j++) {
		        trust[i] += Integer.parseInt(line[j]) << j;
		    }
		}
		cost = new int[n];
		String[] line = br.readLine().split(" ", n);
		for (int i = 0; i < n; i++) {
		    cost[i] = Integer.parseInt(line[i]);
		}
		dp = new int[1 << n];
		Arrays.fill(dp, -1);
		System.out.println(dfw((1 << n) - 1));
    }
    
    static int dfw(int key) {
        if (key == 0) {
            return 0;
        }
        if (dp[key] != -1) {
            return dp[key];
        } 
        int min = Integer.MAX_VALUE / 10;
        for (int i = 0; i < n; i++) {
            if (((1 << i) & key) == 0) {
                continue;
            }
            int add = 0;
            if ((key & trust[i]) != trust[i]) {
                add += cost[i];
            }
            min = Math.min(min, dfw(key ^ (1 << i)) + add);
        }
        dp[key] = min;
        return min;
    }
}
0