From 380470d359ea0a3ce8ac2751a78695932c9124ac Mon Sep 17 00:00:00 2001 From: oncsr Date: Wed, 12 Feb 2025 16:05:19 +0900 Subject: [PATCH] =?UTF-8?q?[20250212]=20BOJ=20/=20P5=20/=20=EB=91=90=20?= =?UTF-8?q?=EA=B0=80=EC=A4=91=EC=B9=98=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0 \352\260\200\354\244\221\354\271\230.md" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "khj20006/202502/12 BOJ P5 \353\221\220 \352\260\200\354\244\221\354\271\230.md" diff --git "a/khj20006/202502/12 BOJ P5 \353\221\220 \352\260\200\354\244\221\354\271\230.md" "b/khj20006/202502/12 BOJ P5 \353\221\220 \352\260\200\354\244\221\354\271\230.md" new file mode 100644 index 00000000..373d7366 --- /dev/null +++ "b/khj20006/202502/12 BOJ P5 \353\221\220 \352\260\200\354\244\221\354\271\230.md" @@ -0,0 +1,87 @@ +```java + +import java.util.*; +import java.io.*; + +class Main { + + // IO field + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + static long nextLong() {return Long.parseLong(st.nextToken());} + static void bwEnd() throws Exception {bw.flush();bw.close();} + + // Additional field + static int[][] D; + static int[][] cost1; + static int[][] cost2; + + static int N; + static final int INF = (int)1e9 + 5; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + } + + static void ready() throws Exception{ + + N = Integer.parseInt(br.readLine()); + D = new int[N][N*9+1]; + for(int i=0;i PQ = new PriorityQueue<>((a,b) -> a[0]-b[0]); + PQ.offer(new int[] {0,0,0}); + D[0][0] = 0; + while(!PQ.isEmpty()) { + int[] now = PQ.poll(); + int w = now[0], s = now[1], n = now[2]; + if(w > D[n][s]) continue; + for(int i=0;i w + cost2[n][i]) { + D[i][s+cost1[n][i]] = w + cost2[n][i]; + PQ.offer(new int[] {w+cost2[n][i],s+cost1[n][i],i}); + } + } + } + + int ans = INF; + for(int i=0;i<=N*9;i++) if(D[1][i] != INF) ans = Math.min(ans, D[1][i]*i); + bw.write((ans==INF ? -1 : ans) + "\n"); + + } + + +} + +```