- 난이도: Lv2
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/12978
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/2/배달
풀이 시간 : 27분
import java.util.*;
class Solution {
public int solution(int N, int[][] road, int K) {
List<List<int[]>> graph = new ArrayList<>();
for (int i = 0; i <= N; i++) graph.add(new ArrayList<>());
for (int[] r : road) {
graph.get(r[0]).add(new int[]{r[1], r[2]});
graph.get(r[1]).add(new int[]{r[0], r[2]});
}
int[] dist = new int[N + 1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[1] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
pq.offer(new int[]{1, 0});
while (!pq.isEmpty()) {
int[] cur = pq.poll();
int now = cur[0], cost = cur[1];
if (cost > dist[now]) continue;
for (int[] next : graph.get(now)) {
int to = next[0], time = next[1];
if (dist[to] > cost + time) {
dist[to] = cost + time;
pq.offer(new int[]{to, dist[to]});
}
}
}
int count = 0;
for (int i = 1; i <= N; i++) {
if (dist[i] <= K) count++;
}
return count;
}
}