結果

問題 No.943 取り調べ
ユーザー ks2mks2m
提出日時 2019-12-06 00:25:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 611 ms / 1,206 ms
コード長 1,719 bytes
コンパイル時間 5,145 ms
コンパイル使用メモリ 74,528 KB
実行使用メモリ 57,492 KB
最終ジャッジ日時 2023-08-24 19:29:56
合計ジャッジ時間 8,119 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,248 KB
testcase_01 AC 42 ms
49,488 KB
testcase_02 AC 43 ms
49,296 KB
testcase_03 AC 43 ms
49,420 KB
testcase_04 AC 426 ms
56,816 KB
testcase_05 AC 610 ms
57,260 KB
testcase_06 AC 611 ms
57,044 KB
testcase_07 AC 42 ms
49,056 KB
testcase_08 AC 407 ms
56,568 KB
testcase_09 AC 175 ms
56,512 KB
testcase_10 AC 231 ms
57,284 KB
testcase_11 AC 166 ms
56,552 KB
testcase_12 AC 43 ms
49,288 KB
testcase_13 AC 49 ms
49,432 KB
testcase_14 AC 56 ms
51,764 KB
testcase_15 AC 45 ms
49,192 KB
testcase_16 AC 44 ms
49,252 KB
testcase_17 AC 256 ms
57,224 KB
testcase_18 AC 44 ms
49,240 KB
testcase_19 AC 47 ms
49,288 KB
testcase_20 AC 43 ms
49,240 KB
testcase_21 AC 42 ms
49,328 KB
testcase_22 AC 241 ms
56,672 KB
testcase_23 AC 564 ms
57,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Arrays;
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;
			boolean[] rem = new boolean[n];
			Arrays.fill(rem, true);
			Queue<Integer> que = new ArrayDeque<>();
			for (int j = 0; j < n; j++) {
				if ((x & 1) == 1) {
					val += a[j];
					arr[j] = 0;
					rem[j] = false;
					que.add(j);
				}
				x /= 2;
			}
			for (int k = 0; k < n; k++) {
				if (rem[k] && arr[k] == 0) {
					rem[k] = false;
					que.add(k);
				}
			}
			while (!que.isEmpty()) {
				int j = que.poll();
				for (int k = 0; k < n; k++) {
					if (rem[k]) {
						arr[k] &= (all - (1 << j));
						if (arr[k] == 0) {
							rem[k] = false;
							que.add(k);
						}
					}
				}
			}
			boolean flg = true;
			for (int k = 0; k < n; k++) {
				if (rem[k]) {
					flg = false;
					break;
				}
			}
			if (flg) {
				ans = Math.min(ans, val);
			}
		}
		System.out.println(ans);
	}
}
0