結果

問題 No.943 取り調べ
ユーザー ks2mks2m
提出日時 2019-12-05 23:58:48
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,652 bytes
コンパイル時間 2,400 ms
コンパイル使用メモリ 74,724 KB
実行使用メモリ 61,564 KB
最終ジャッジ日時 2023-08-24 19:28:48
合計ジャッジ時間 12,340 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,368 KB
testcase_01 AC 43 ms
49,388 KB
testcase_02 AC 43 ms
49,604 KB
testcase_03 AC 44 ms
49,384 KB
testcase_04 AC 895 ms
60,628 KB
testcase_05 AC 1,193 ms
61,376 KB
testcase_06 AC 1,127 ms
61,428 KB
testcase_07 AC 42 ms
49,072 KB
testcase_08 AC 897 ms
60,524 KB
testcase_09 AC 667 ms
61,064 KB
testcase_10 AC 432 ms
59,580 KB
testcase_11 AC 485 ms
61,388 KB
testcase_12 AC 42 ms
49,492 KB
testcase_13 AC 75 ms
52,164 KB
testcase_14 AC 79 ms
52,132 KB
testcase_15 AC 48 ms
50,768 KB
testcase_16 AC 42 ms
49,268 KB
testcase_17 AC 539 ms
60,328 KB
testcase_18 AC 45 ms
49,392 KB
testcase_19 AC 74 ms
51,700 KB
testcase_20 AC 48 ms
50,936 KB
testcase_21 AC 43 ms
49,384 KB
testcase_22 AC 586 ms
60,556 KB
testcase_23 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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;
			Set<Integer> set = new HashSet<>();
			for (int j = 0; j < n; j++) {
				set.add(j);
			}
			Queue<Integer> que = new ArrayDeque<>();
			for (int j = 0; j < n; j++) {
				if ((x & 1) == 1) {
					val += a[j];
					arr[j] = 0;
					set.remove(j);
					que.add(j);
				}
				x /= 2;
			}
			for (int k : set.toArray(new Integer[0])) {
				if (arr[k] == 0) {
					set.remove(k);
					que.add(k);
				}
			}
			while (!que.isEmpty()) {
				int j = que.poll();
				for (int k : set.toArray(new Integer[0])) {
					arr[k] &= (all - (1 << j));
					if (arr[k] == 0) {
						set.remove(k);
						que.add(k);
					}
				}
			}
			if (set.isEmpty()) {
				ans = Math.min(ans, val);
			}
		}
		System.out.println(ans);
	}
}
0