|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static int[][] groups = { |
| 9 | + {0, 0}, {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12} |
| 10 | + }; |
| 11 | + private static int[] combination, firstInGroup; |
| 12 | + private static boolean[] visited; |
| 13 | + private static int[][] times; |
| 14 | + private static int minTime = Integer.MAX_VALUE; |
| 15 | +
|
| 16 | + public static void main(String[] args) throws IOException { |
| 17 | + init(); |
| 18 | +
|
| 19 | + DFS(1); |
| 20 | + bw.write(minTime + "\n"); |
| 21 | + bw.flush(); |
| 22 | + bw.close(); |
| 23 | + br.close(); |
| 24 | + } |
| 25 | +
|
| 26 | + private static void init() throws IOException { |
| 27 | + times = new int[13][13]; |
| 28 | + combination = new int[7]; |
| 29 | + firstInGroup = new int[7]; |
| 30 | + visited = new boolean[7]; |
| 31 | +
|
| 32 | + for (int i = 1; i <= 12; i++) { |
| 33 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 34 | + for (int j = 1; j <= 12; j++) { |
| 35 | + times[i][j] = Integer.parseInt(st.nextToken()); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | +
|
| 40 | + private static void DFS(int index) { |
| 41 | + if (index == 7) { |
| 42 | + for (int mask = 0; mask < (1 << 6); mask++) { |
| 43 | + for (int i = 1; i <= 6; i++) { |
| 44 | + firstInGroup[i] = ((mask >> (i - 1)) & 1) == 0 ? 0 : 1; |
| 45 | + } |
| 46 | + int totalTime = cal(); |
| 47 | + minTime = Math.min(minTime, totalTime); |
| 48 | + } |
| 49 | + return; |
| 50 | + } |
| 51 | +
|
| 52 | + for (int i = 1; i <= 6; i++) { |
| 53 | + if (!visited[i]) { |
| 54 | + visited[i] = true; |
| 55 | + combination[index] = i; |
| 56 | + DFS(index + 1); |
| 57 | + visited[i] = false; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + private static int cal() { |
| 63 | + int totalTime = 0; |
| 64 | + int prevStudent = -1; |
| 65 | +
|
| 66 | + for (int i = 1; i <= 6; i++) { |
| 67 | + int index = combination[i]; |
| 68 | + int[] group = groups[index]; |
| 69 | + int first = group[firstInGroup[index]]; |
| 70 | + int second = group[1 - firstInGroup[index]]; |
| 71 | +
|
| 72 | + if (i == 1) { |
| 73 | + totalTime += times[first][second]; |
| 74 | + prevStudent = second; |
| 75 | + } else { |
| 76 | + totalTime += times[prevStudent][first]; |
| 77 | + totalTime += times[first][second]; |
| 78 | + prevStudent = second; |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | + return totalTime; |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
0 commit comments