結果

問題 No.943 取り調べ
ユーザー tenten
提出日時 2020-08-27 01:12:20
言語 Java
(openjdk 23)
結果
AC  
実行時間 243 ms / 1,206 ms
コード長 1,221 bytes
コンパイル時間 2,775 ms
コンパイル使用メモリ 77,752 KB
実行使用メモリ 42,848 KB
最終ジャッジ日時 2024-11-07 14:23:37
合計ジャッジ時間 7,274 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static int n;
    static int[] keys;
    static int[] costs;
    static int[] dp;
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	n = sc.nextInt();
    	keys = new int[n];
    	for (int i = n - 1; i >= 0; i--) {
    	    for (int j = 0; j < n; j++) {
    	        keys[i] <<= 1;
    	        keys[i] += sc.nextInt();
    	    }
    	}
    	costs = new int[n];
    	for (int i = n - 1; i >= 0; i--) {
    	    costs[i] = sc.nextInt();
    	}
    	dp = new int[1 << n];
    	System.out.println(dfw((1 << n) - 1));
    }
    
    static int dfw(int mask) {
        if (mask == 0) {
            return 0;
        }
        if (dp[mask] == 0) {
            int min = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++) {
                if (((1 << i) & mask) == 0) {
                    continue;
                }
                if ((keys[i] & mask) == keys[i]) {
                    min = Math.min(min, dfw(mask ^ (1 << i)));
                } else {
                    min = Math.min(min, dfw(mask ^ (1 << i)) + costs[i]);
                }
            }
            dp[mask] = min;
        }
        return dp[mask];
    }
}

0