|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.StringTokenizer; |
| 4 | + |
| 5 | +public class BJ_25835_빠른_무작위_메시지_전달 { |
| 6 | + |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static StringTokenizer st; |
| 10 | + |
| 11 | + private static int ans; |
| 12 | + private static int[][] friends; |
| 13 | + private static boolean[] visited; |
| 14 | + |
| 15 | + public static void main(String[] args) throws IOException { |
| 16 | + init(); |
| 17 | + sol(); |
| 18 | + |
| 19 | + bw.write(ans + ""); |
| 20 | + bw.flush(); |
| 21 | + bw.close(); |
| 22 | + br.close(); |
| 23 | + } |
| 24 | + |
| 25 | + private static void init() throws IOException { |
| 26 | + ans = Integer.MAX_VALUE; |
| 27 | + |
| 28 | + friends = new int[12][12]; |
| 29 | + for (int i = 0; i < 12; i++) { |
| 30 | + st = new StringTokenizer(br.readLine()); |
| 31 | + for (int j = 0; j < 12; j++) { |
| 32 | + friends[i][j] = Integer.parseInt(st.nextToken()); |
| 33 | + } |
| 34 | + } |
| 35 | + visited = new boolean[6]; |
| 36 | + } |
| 37 | + |
| 38 | + private static void sol() throws IOException { |
| 39 | + for (int nextGroup = 0; nextGroup < 6; nextGroup++) { |
| 40 | + int next = nextGroup * 2; |
| 41 | + |
| 42 | + visited[nextGroup] = true; |
| 43 | + dfs(1, next + 1, friends[next][next + 1]); |
| 44 | + dfs(1, next, friends[next + 1][next]); |
| 45 | + visited[nextGroup] = false; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static void dfs(int depth, int cur, int sum) throws IOException { |
| 50 | + if (depth == 6) { |
| 51 | + ans = Math.min(ans, sum); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + for (int i = 0; i < 6; i++) { |
| 56 | + if (visited[i]) continue; |
| 57 | + |
| 58 | + int next = i * 2; |
| 59 | + |
| 60 | + visited[i] = true; |
| 61 | + dfs(depth + 1, next, sum + friends[cur][next + 1] + friends[next + 1][next]); |
| 62 | + dfs(depth + 1, next + 1, sum + friends[cur][next] + friends[next][next + 1]); |
| 63 | + visited[i] = false; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | +``` |
0 commit comments