-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
63 lines (52 loc) Β· 1.79 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package BruteForce.swea4012;
import java.io.*;
import java.util.*;
public class Solution {
static int T, N, ans;
static int[][] map;
static boolean[] select;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BruteForce/swea4012/sample_input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) map[i][j] = Integer.parseInt(st.nextToken());
}
ans = Integer.MAX_VALUE;
select = new boolean[N];
comb(1, 0);
sb.append("#").append(t).append(" ").append(ans).append("\n");
}
System.out.println(sb.toString());
}
static void comb(int idx, int cnt) {
if (cnt == N/2) {
int[] m1 = new int[N/2], m2 = new int[N/2];
for (int i = 0, j = 0, k = 0; i < N; i++) {
if (select[i]) m1[j++] = i;
else m2[k++] = i;
}
ans = Math.min(ans, Math.abs(calS(m1) - calS(m2)));
return;
}
for (int i = idx; i < N; i++) {
select[i] = true;
comb(i+1, cnt+1);
select[i] = false;
}
}
static int calS(int[] m) {
int s = 0;
for (int i = 0; i < N/2-1; i++) {
for (int j = i; j < N/2; j++) {
s += map[m[i]][m[j]] + map[m[j]][m[i]];
}
}
return s;
}
}