Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified kokeunho/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions kokeunho/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
---
84 changes: 84 additions & 0 deletions kokeunho/๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰/31-kokeunho.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import java.util.*;

public class Main {
static int N, M, K;
static List<List<Node>> graph;
static class Node implements Comparable<Node> {
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<Node> 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;
}
}