|
| 1 | +package BruteForce.P14889; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + static int N, ans = Integer.MAX_VALUE; |
| 9 | + static int[][] S; |
| 10 | + static boolean[] visited; |
| 11 | + |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + System.setIn(new FileInputStream("src/BruteForce/P14889/input.txt")); |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + |
| 16 | + N = Integer.parseInt(br.readLine()); |
| 17 | + S = new int[N+1][N+1]; |
| 18 | + for (int i = 1; i <= N; i++) { |
| 19 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 20 | + for (int j = 1; j <= N; j++) S[i][j] = Integer.parseInt(st.nextToken()); |
| 21 | + } |
| 22 | + |
| 23 | + visited = new boolean[N+1]; |
| 24 | + visited[1] = true; |
| 25 | + comb(2, 1); |
| 26 | + |
| 27 | + System.out.println(ans); |
| 28 | + } |
| 29 | + |
| 30 | + static void comb(int s, int cnt) { |
| 31 | + if (cnt == N/2) { |
| 32 | + calc(); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + for (int i = s; i <= N; i++) { |
| 37 | + visited[i] = true; |
| 38 | + comb(i+1, cnt+1); |
| 39 | + visited[i] = false; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static void calc() { |
| 44 | + int s1 = 0, s2 = 0; |
| 45 | + int[] t1 = new int[N/2], t2 = new int[N/2]; |
| 46 | + |
| 47 | + for (int i = 1, k = 0, l = 0; i <= N; i++) { |
| 48 | + if (visited[i]) t1[k++] = i; |
| 49 | + else t2[l++] = i; |
| 50 | + } |
| 51 | + |
| 52 | + for (int i = 0; i < N/2-1; i++) { |
| 53 | + for (int j = i; j < N/2; j++) { |
| 54 | + s1 += S[t1[i]][t1[j]] + S[t1[j]][t1[i]]; |
| 55 | + s2 += S[t2[i]][t2[j]] + S[t2[j]][t2[i]]; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + ans = Math.min(ans, Math.abs(s1-s2)); |
| 60 | + } |
| 61 | +} |
0 commit comments