結果

問題 No.943 取り調べ
ユーザー ks2mks2m
提出日時 2019-12-06 00:18:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 547 ms / 1,206 ms
コード長 1,569 bytes
コンパイル時間 3,235 ms
コンパイル使用メモリ 74,672 KB
実行使用メモリ 57,356 KB
最終ジャッジ日時 2023-08-24 19:29:43
合計ジャッジ時間 7,502 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,672 KB
testcase_01 AC 42 ms
49,380 KB
testcase_02 AC 42 ms
49,132 KB
testcase_03 AC 41 ms
49,256 KB
testcase_04 AC 373 ms
57,196 KB
testcase_05 AC 547 ms
57,132 KB
testcase_06 AC 517 ms
57,356 KB
testcase_07 AC 45 ms
49,332 KB
testcase_08 AC 340 ms
56,876 KB
testcase_09 AC 193 ms
56,832 KB
testcase_10 AC 165 ms
56,148 KB
testcase_11 AC 162 ms
56,892 KB
testcase_12 AC 43 ms
49,292 KB
testcase_13 AC 47 ms
49,204 KB
testcase_14 AC 58 ms
51,140 KB
testcase_15 AC 43 ms
49,236 KB
testcase_16 AC 43 ms
49,428 KB
testcase_17 AC 199 ms
56,852 KB
testcase_18 AC 43 ms
49,348 KB
testcase_19 AC 47 ms
49,680 KB
testcase_20 AC 43 ms
49,736 KB
testcase_21 AC 42 ms
49,232 KB
testcase_22 AC 230 ms
56,960 KB
testcase_23 AC 442 ms
57,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int[] arr0 = new int[n];
		for (int i = 0; i < n; i++) {
			String[] sa = br.readLine().split(" ");
			for (int j = 0; j < n; j++) {
				if (Integer.parseInt(sa[j]) == 1) {
					arr0[i] += 1 << j;
				}
			}
		}
		String[] sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		int ans = Integer.MAX_VALUE;
		int end = 1 << n;
		int all = end - 1;
		for (int i = 0; i < end; i++) {
			int[] arr = new int[n];
			System.arraycopy(arr0, 0, arr, 0, n);

			int val = 0;
			int x = i;
			int rem = all;
			Queue<Integer> que = new ArrayDeque<>();
			for (int j = 0; j < n; j++) {
				if ((x & 1) == 1) {
					val += a[j];
					arr[j] = 0;
					rem -= (1 << j);
					que.add(j);
				}
				x /= 2;
			}
			for (int k = 0; k < n; k++) {
				if ((rem >> k & 1) == 1 && arr[k] == 0) {
					rem -= (1 << k);
					que.add(k);
				}
			}
			while (!que.isEmpty()) {
				int j = que.poll();
				for (int k = 0; k < n; k++) {
					if ((rem >> k & 1) == 1) {
						arr[k] &= (all - (1 << j));
						if (arr[k] == 0) {
							rem -= (1 << k);
							que.add(k);
						}
					}
				}
			}
			if (rem == 0) {
				ans = Math.min(ans, val);
			}
		}
		System.out.println(ans);
	}
}
0