From 0f410987d9083995664f6ac6f1c101ed3437e942 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Wed, 27 Aug 2025 12:45:10 +0900 Subject: [PATCH] =?UTF-8?q?[20250827]=20BOJ=20/=20P5=20/=20=EB=8F=84?= =?UTF-8?q?=EB=A1=9C=ED=8F=AC=EC=9E=A5=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\353\241\234\355\217\254\354\236\245.md" | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 "lkhyun/202508/27 BOJ P5 \353\217\204\353\241\234\355\217\254\354\236\245.md" diff --git "a/lkhyun/202508/27 BOJ P5 \353\217\204\353\241\234\355\217\254\354\236\245.md" "b/lkhyun/202508/27 BOJ P5 \353\217\204\353\241\234\355\217\254\354\236\245.md" new file mode 100644 index 00000000..88897eea --- /dev/null +++ "b/lkhyun/202508/27 BOJ P5 \353\217\204\353\241\234\355\217\254\354\236\245.md" @@ -0,0 +1,88 @@ +```java +import java.util.*; +import java.io.*; + +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 StringBuilder sb = new StringBuilder(); + static int N,M,K; + static class Edge{ + int to,cost; + + Edge(int to, int cost){ + this.to = to; + this.cost = cost; + } + } + static class State{ + int to,k; + long cost; + State(int to, int k, long cost){ + this.to = to; + this.k = k; + this.cost = cost; + } + } + static List[] adjList; + + public static void main(String[] args) throws Exception { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + adjList = new ArrayList[N+1]; + + for (int i = 1; i <= N; i++) { + adjList[i] = new ArrayList<>(); + } + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + adjList[from].add(new Edge(to, cost)); + adjList[to].add(new Edge(from, cost)); + } + long[][] dist = dijkstra(); + long ans = Long.MAX_VALUE; + for (int i = 0; i <= K; i++) { + ans = Math.min(ans, dist[N][i]); + } + bw.write(ans+""); + bw.close(); + } + public static long[][] dijkstra(){ + PriorityQueue pq = new PriorityQueue<>((a,b) -> Long.compare(a.cost,b.cost)); + long[][] dist = new long[N+1][K+1]; + for (int i = 1; i <= N; i++) { + Arrays.fill(dist[i],Long.MAX_VALUE); + } + pq.offer(new State(1,0,0)); + dist[1][0] = 0; + + while(!pq.isEmpty()){ + State cur = pq.poll(); + + if(dist[cur.to][cur.k] < cur.cost) continue; + + for(Edge e : adjList[cur.to]){ + long notUsedCost = cur.cost + e.cost; + long usedCost = cur.cost; + + if(notUsedCost < dist[e.to][cur.k]){ + dist[e.to][cur.k] = notUsedCost; + pq.offer(new State(e.to, cur.k, notUsedCost)); + } + + if(cur.k < K && usedCost < dist[e.to][cur.k + 1]){ + dist[e.to][cur.k + 1] = usedCost; + pq.offer(new State(e.to, cur.k + 1, usedCost)); + } + } + } + return dist; + } +} +```