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"); + + } + + +} + +```