프로그래머스 | 배달

커몽·2021년 7월 3일
0

프로그래머스 level2

목록 보기
31/38
post-custom-banner
function solution(N, road, K) {
  var answer = 0;
  let visit = new Array(N + 1).fill(Infinity);
  let adj = Array.from(Array(N + 1), () => Array());
  for (let i = 0; i < road.length; i++) {
    let [A, B, time] = road[i];
    adj[A].push({ to: B, time: time })
    adj[B].push({ to: A, time: time })
  }
  visit[1] = 0;
  let check = [{ to: 1, time: 0 }];
  while (check.length) {
    let temp = check.pop();
    adj[temp.to].forEach(e => {
      if (visit[e.to] > visit[temp.to] + e.time) {

        visit[e.to] = visit[temp.to] + e.time
        check.push(e);
      }
    });


  }
  visit.forEach(e => e <= K && answer++);
  return answer
}
post-custom-banner

0개의 댓글