From 1a5b97b1b6168ec8503e23359ded5e1234dafefb Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Sun, 22 Jun 2025 16:35:55 +0900 Subject: [PATCH] =?UTF-8?q?[20250622]=20BOJ=20/=20G1=20/=20=EC=99=B8?= =?UTF-8?q?=ED=8C=90=EC=9B=90=20=EC=88=9C=ED=9A=8C=20/=20=EC=9D=B4?= =?UTF-8?q?=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\233\220 \354\210\234\355\232\214.md" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 "lkhyun/202506/22 BOJ G1 \354\231\270\355\214\220\354\233\220 \354\210\234\355\232\214.md" diff --git "a/lkhyun/202506/22 BOJ G1 \354\231\270\355\214\220\354\233\220 \354\210\234\355\232\214.md" "b/lkhyun/202506/22 BOJ G1 \354\231\270\355\214\220\354\233\220 \354\210\234\355\232\214.md" new file mode 100644 index 00000000..ca3b0eee --- /dev/null +++ "b/lkhyun/202506/22 BOJ G1 \354\231\270\355\214\220\354\233\220 \354\210\234\355\232\214.md" @@ -0,0 +1,57 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int N; + static int[][] cost; + static int[][] dp; + + public static void main(String[] args) throws IOException { + N = Integer.parseInt(br.readLine()); + cost = new int[N][N]; + dp = new int[1 << N][N]; + + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < N; j++) { + cost[i][j] = Integer.parseInt(st.nextToken()); + } + } + + for (int i = 0; i < (1 << N); i++) { + Arrays.fill(dp[i], Integer.MAX_VALUE); + } + + dp[1][0] = 0; + + for (int i = 1; i < (1 << N); i++) { + for (int j = 0; j < N; j++) { + if ((i & (1 << j)) == 0) continue; + if (dp[i][j] == Integer.MAX_VALUE) continue; + + for (int k = 0; k < N; k++) { + if ((i & (1 << k)) != 0) continue; + if (cost[j][k] == 0) continue; + + dp[i | (1 << k)][k] = Math.min(dp[i | (1 << k)][k], dp[i][j] + cost[j][k]); + } + } + } + + int answer = Integer.MAX_VALUE; + + for (int i = 1; i < N; i++) { + if (dp[(1 << N) - 1][i] != Integer.MAX_VALUE && cost[i][0] != 0) { + answer = Math.min(answer, dp[(1 << N) - 1][i] + cost[i][0]); + } + } + + bw.write(answer + ""); + bw.close(); + } +} +```