-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
61 lines (48 loc) Β· 1.57 KB
/
Main.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
package BruteForce.P14889;
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int N, ans = Integer.MAX_VALUE;
static int[][] S;
static boolean[] visited;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BruteForce/P14889/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
S = new int[N+1][N+1];
for (int i = 1; i <= N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 1; j <= N; j++) S[i][j] = Integer.parseInt(st.nextToken());
}
visited = new boolean[N+1];
visited[1] = true;
comb(2, 1);
System.out.println(ans);
}
static void comb(int s, int cnt) {
if (cnt == N/2) {
calc();
return;
}
for (int i = s; i <= N; i++) {
visited[i] = true;
comb(i+1, cnt+1);
visited[i] = false;
}
}
static void calc() {
int s1 = 0, s2 = 0;
int[] t1 = new int[N/2], t2 = new int[N/2];
for (int i = 1, k = 0, l = 0; i <= N; i++) {
if (visited[i]) t1[k++] = i;
else t2[l++] = i;
}
for (int i = 0; i < N/2-1; i++) {
for (int j = i; j < N/2; j++) {
s1 += S[t1[i]][t1[j]] + S[t1[j]][t1[i]];
s2 += S[t2[i]][t2[j]] + S[t2[j]][t2[i]];
}
}
ans = Math.min(ans, Math.abs(s1-s2));
}
}