diff --git a/kokeunho/.DS_Store b/kokeunho/.DS_Store index 714ee42..1256d34 100644 Binary files a/kokeunho/.DS_Store and b/kokeunho/.DS_Store differ diff --git a/kokeunho/README.md b/kokeunho/README.md index 5c5f6cc..c4b912d 100644 --- a/kokeunho/README.md +++ b/kokeunho/README.md @@ -31,4 +31,5 @@ | 28차시 | 2025.07.01 | 구현 | [스도쿠](https://www.acmicpc.net/problem/2580) |[#116](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/116) | | 29차시 | 2025.07.21 | 그래프 탐색 | [불!](https://www.acmicpc.net/problem/4179) |[#118](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/118) | | 30차시 | 2025.07.26 | 그래프 탐색 | [중량제한](https://www.acmicpc.net/problem/1939) |[#120](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/120) | +| 31차시 | 2025.08.03 | 그래프 탐색 | [면접보는 승범이네](https://www.acmicpc.net/problem/17835) | [#126](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/126) | --- diff --git "a/kokeunho/\352\267\270\353\236\230\355\224\204 \355\203\220\354\203\211/31-kokeunho.java" "b/kokeunho/\352\267\270\353\236\230\355\224\204 \355\203\220\354\203\211/31-kokeunho.java" new file mode 100644 index 0000000..c5ed3af --- /dev/null +++ "b/kokeunho/\352\267\270\353\236\230\355\224\204 \355\203\220\354\203\211/31-kokeunho.java" @@ -0,0 +1,84 @@ +import java.util.*; + +public class Main { + static int N, M, K; + static List> graph; + static class Node implements Comparable { + int to; + long distance; + + public Node (int to, long distance) { + this.to = to; + this.distance = distance; + } + + @Override + public int compareTo(Node o) { + return (int)(this.distance - o.distance); + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + N = sc.nextInt(); + M = sc.nextInt(); + K = sc.nextInt(); + + graph = new ArrayList<>(); + for (int i = 0; i <= N; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < M; i++) { + int from = sc.nextInt(); + int to = sc.nextInt(); + long distance = sc.nextLong(); + graph.get(to).add(new Node(from, distance)); + } + int[] interviewCities = new int[K]; + for (int i = 0; i < K; i++) { + interviewCities[i] = sc.nextInt(); + } + + long[] dist = dijkstra(interviewCities); + + int farthestCity = 0; + long longestDist = -1; + for (int i = 1; i <= N; i++) { + if (dist[i] > longestDist) { + longestDist = dist[i]; + farthestCity = i; + } + } + System.out.print(farthestCity + "\n" + longestDist); + } + static long[] dijkstra(int[] interviewCities) { + long[] dist = new long[N+1]; + Arrays.fill(dist, Long.MAX_VALUE); + + PriorityQueue pq = new PriorityQueue<>(); + + for (int city : interviewCities) { + dist[city] = 0; + pq.offer(new Node(city, 0)); + } + + while (!pq.isEmpty()) { + Node current = pq.poll(); + int now = current.to; + long distance = current.distance; + + if (distance > dist[now]) continue; + + for (Node next : graph.get(now)) { + long newDistance = dist[now] + next.distance; + if (newDistance < dist[next.to]) { + pq.offer(new Node(next.to, newDistance)); + dist[next.to] = newDistance; + } + } + } + return dist; + } +} \ No newline at end of file