結果

問題 No.943 取り調べ
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-12-05 23:01:56
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,016 bytes
コンパイル時間 4,364 ms
コンパイル使用メモリ 87,960 KB
実行使用メモリ 156,860 KB
最終ジャッジ日時 2023-08-24 18:48:33
合計ジャッジ時間 7,160 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
55,824 KB
testcase_01 AC 67 ms
51,812 KB
testcase_02 AC 65 ms
51,592 KB
testcase_03 AC 66 ms
51,724 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package _0943;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class Main {
    private int n;
    private int[][] x;
    private int[] a;

    public void solve(BufferedReader stdin, PrintWriter stdout) throws NumberFormatException, IOException {
        Pattern space = Pattern.compile(" ");
        n = Integer.parseInt(stdin.readLine());
        x = new int[n][n];
        for (int i = 0; i < n; i++) {
            x[i] = space.splitAsStream(stdin.readLine()).mapToInt(Integer::parseInt).toArray();
        }
        a = space.splitAsStream(stdin.readLine()).mapToInt(Integer::parseInt).toArray();

        int bit = 0;
        for (int i = 0; i < n; i++) {
            if (Arrays.stream(x[i]).allMatch(v -> v == 0)) {
                bit |= 1 << i;
            }
        }

        stdout.println(f(bit, 0));
    }

    private Map<Tuple, Integer> cache = new HashMap<>();

    private int f(int b1, int b2) {
        if (b1 == (1 << n) - 1) {
            return 0;
        }

        Tuple t = new Tuple();
        t.x = b1;
        t.y = b2;
        if (cache.containsKey(t)) {
            return cache.get(t);
        }

        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            if ((b1 & (1 << i)) != 0) {
                continue;
            }

            int c1 = b1 | (1 << i);
            int c2 = b2;
            int d = 0;
            for (int j = 0; j < n; j++) {
                if (x[i][j] == 1
                        && (b1 & (1 << j)) == 0
                        && (b2 & (1 << j)) == 0) {
                    c2 |= 1 << j;
                    d += a[j];
                }
            }

            ans = Math.min(ans, f(c1, c2) + d);
        }

        cache.put(t, ans);
        return ans;
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter stdout = new PrintWriter(System.out, false);
        new Main().solve(stdin, stdout);
        stdout.flush();
    }

    private static class Tuple {
        private int x;
        private int y;
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + x;
            result = prime * result + y;
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Tuple other = (Tuple) obj;
            if (x != other.x)
                return false;
            if (y != other.y)
                return false;
            return true;
        }
    }
}
0