結果
| 問題 |
No.943 取り調べ
|
| コンテスト | |
| ユーザー |
neko_the_shadow
|
| 提出日時 | 2019-12-05 23:01:56 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,016 bytes |
| コンパイル時間 | 4,250 ms |
| コンパイル使用メモリ | 89,600 KB |
| 実行使用メモリ | 147,856 KB |
| 最終ジャッジ日時 | 2024-12-23 03:30:59 |
| 合計ジャッジ時間 | 29,541 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 14 TLE * 10 |
ソースコード
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;
}
}
}
neko_the_shadow