結果

問題 No.943 取り調べ
ユーザー tentententen
提出日時 2020-08-27 01:12:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 245 ms / 1,206 ms
コード長 1,221 bytes
コンパイル時間 2,309 ms
コンパイル使用メモリ 77,648 KB
実行使用メモリ 43,132 KB
最終ジャッジ日時 2024-04-25 04:26:01
合計ジャッジ時間 7,407 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
41,112 KB
testcase_01 AC 137 ms
41,092 KB
testcase_02 AC 137 ms
41,084 KB
testcase_03 AC 120 ms
40,168 KB
testcase_04 AC 199 ms
42,760 KB
testcase_05 AC 206 ms
43,132 KB
testcase_06 AC 206 ms
42,716 KB
testcase_07 AC 137 ms
41,356 KB
testcase_08 AC 198 ms
42,840 KB
testcase_09 AC 185 ms
41,876 KB
testcase_10 AC 176 ms
42,148 KB
testcase_11 AC 162 ms
41,804 KB
testcase_12 AC 136 ms
41,268 KB
testcase_13 AC 147 ms
41,348 KB
testcase_14 AC 146 ms
41,272 KB
testcase_15 AC 142 ms
41,360 KB
testcase_16 AC 135 ms
41,140 KB
testcase_17 AC 245 ms
41,936 KB
testcase_18 AC 140 ms
41,216 KB
testcase_19 AC 141 ms
41,372 KB
testcase_20 AC 140 ms
41,036 KB
testcase_21 AC 138 ms
41,436 KB
testcase_22 AC 178 ms
41,868 KB
testcase_23 AC 203 ms
42,496 KB
権限があれば一括ダウンロードができます

ソースコード

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