From 3dd8f77418c2c34c357be4aeee03d77e70454b6d Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Tue, 30 Sep 2025 22:07:04 +0900 Subject: [PATCH] =?UTF-8?q?[20250930]=20PGM=20/=20Lv3=20/=20=ED=95=A9?= =?UTF-8?q?=EC=8A=B9=20=ED=83=9D=EC=8B=9C=20=EC=9A=94=EA=B8=88=20/=20?= =?UTF-8?q?=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\354\213\234 \354\232\224\352\270\210.md" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 "lkhyun/202509/30 PGM Lv3 \355\225\251\354\212\271 \355\203\235\354\213\234 \354\232\224\352\270\210.md" diff --git "a/lkhyun/202509/30 PGM Lv3 \355\225\251\354\212\271 \355\203\235\354\213\234 \354\232\224\352\270\210.md" "b/lkhyun/202509/30 PGM Lv3 \355\225\251\354\212\271 \355\203\235\354\213\234 \354\232\224\352\270\210.md" new file mode 100644 index 00000000..bcca903f --- /dev/null +++ "b/lkhyun/202509/30 PGM Lv3 \355\225\251\354\212\271 \355\203\235\354\213\234 \354\232\224\352\270\210.md" @@ -0,0 +1,62 @@ +```java +import java.util.*; + +class Solution { + static class Edge{ + int to,cost; + Edge(int to, int cost){ + this.to = to; + this.cost = cost; + } + } + static List[] adjList; + static int[] distA; + static int[] distB; + static int[] distS; + public int solution(int n, int s, int a, int b, int[][] fares) { + int answer = 0; + adjList = new List[n+1]; + for(int i = 1; i<=n; i++){ + adjList[i] = new ArrayList<>(); + } + + for(int[] fare : fares){ + adjList[fare[0]].add(new Edge(fare[1],fare[2])); + adjList[fare[1]].add(new Edge(fare[0],fare[2])); + } + + distA = new int[n+1]; + distB = new int[n+1]; + distS = new int[n+1]; + dijkstra(a,distA); + dijkstra(b,distB); + dijkstra(s,distS); + + answer = distS[a] + distS[b]; + for(int i=1; i<=n; i++){ + answer = Math.min(answer, distS[i] + distA[i] + distB[i]); + } + return answer; + } + public void dijkstra(int start, int[] dist){ + PriorityQueue pq = new PriorityQueue<>((a,b) -> Integer.compare(a.cost,b.cost)); + Arrays.fill(dist, Integer.MAX_VALUE); + pq.offer(new Edge(start,0)); + dist[start] = 0; + + while(!pq.isEmpty()){ + Edge cur = pq.poll(); + + if(dist[cur.to] < cur.cost) continue; + + for(Edge next : adjList[cur.to]){ + int newDist = cur.cost + next.cost; + if(newDist < dist[next.to]){ + dist[next.to] = newDist; + pq.offer(new Edge(next.to,newDist)); + } + } + } + } +} +```