📄문제
문제 설명
N개의 마을로 이루어진 나라가 있습니다. 이 나라의 각 마을에는 1부터 N까지의 번호가 각각 하나씩 부여되어 있습니다. 각 마을은 양방향으로 통행할 수 있는 도로로 연결되어 있는데, 서로 다른 마을 간에 이동할 때는 이 도로를 지나야 합니다. 도로를 지날 때 걸리는 시간은 도로별로 다릅니다. 현재 1번 마을에 있는 음식점에서 각 마을로 음식 배달을 하려고 합니다. 각 마을로부터 음식 주문을 받으려고 하는데, N개의 마을 중에서 K 시간 이하로 배달이 가능한 마을에서만 주문을 받으려고 합니다. 다음은 N = 5, K = 3인 경우의 예시입니다.

위 그림에서 1번 마을에 있는 음식점은 [1, 2, 4, 5] 번 마을까지는 3 이하의 시간에 배달할 수 있습니다. 그러나 3번 마을까지는 3시간 이내로 배달할 수 있는 경로가 없으므로 3번 마을에서는 주문을 받지 않습니다. 따라서 1번 마을에 있는 음식점이 배달 주문을 받을 수 있는 마을은 4개가 됩니다.마을의 개수 N, 각 마을을 연결하는 도로의 정보 road, 음식 배달이 가능한 시간 K가 매개변수로 주어질 때, 음식 주문을 받을 수 있는 마을의 개수를 return 하도록 solution 함수를 완성해주세요.
제한사항
- 마을의 개수 N은 1 이상 50 이하의 자연수입니다.
- road의 길이(도로 정보의 개수)는 1 이상 2,000 이하입니다.
- road의 각 원소는 마을을 연결하고 있는 각 도로의 정보를 나타냅니다.
- road는 길이가 3인 배열이며, 순서대로 (a, b, c)를 나타냅니다.
- a, b(1 ≤ a, b ≤ N, a != b)는 도로가 연결하는 두 마을의 번호이며, c(1 ≤ c ≤ 10,000, c는 자연수)는 도로를 지나는데 걸리는 시간입니다.
- 두 마을 a, b를 연결하는 도로는 여러 개가 있을 수 있습니다.
- 한 도로의 정보가 여러 번 중복해서 주어지지 않습니다.
- K는 음식 배달이 가능한 시간을 나타내며, 1 이상 500,000 이하입니다.
- 임의의 두 마을간에 항상 이동 가능한 경로가 존재합니다.
- 1번 마을에 있는 음식점이 K 이하의 시간에 배달이 가능한 마을의 개수를 return 하면 됩니다.
✏️풀이
재영 띱!
class MinHeap { constructor() { this.heap = [null]; this.size = 0; } heappush(value) { this.heap.push(value); let nowIndex = this.heap.length - 1; let parentIndex = Math.floor(nowIndex / 2); while (nowIndex > 1 && this.heap[parentIndex] > this.heap[nowIndex]) { this.swap(nowIndex, parentIndex); nowIndex = parentIndex; parentIndex = Math.floor(nowIndex / 2); } this.size += 1; } heappop() { const returnValue = this.heap[1]; if (this.size === 1) { this.size -= 1; return this.heap.pop(); } this.heap[1] = this.heap.pop(); let nowIndex = 1; let leftIndex = nowIndex * 2; let rightIndex = nowIndex * 2 + 1; while ( this.heap[nowIndex] > this.heap[leftIndex] || this.heap[nowIndex] > this.heap[rightIndex] ) { if (this.heap[rightIndex] < this.heap[leftIndex]) { this.swap(nowIndex, rightIndex); nowIndex = rightIndex; } else { this.swap(nowIndex, leftIndex); nowIndex = leftIndex; } leftIndex = nowIndex * 2; rightIndex = nowIndex * 2 + 1; } this.size -= 1; return returnValue; } swap(a, b) { [this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]]; } } const solution = (N, road, K) => { const start = 1; const graph = Array.from({ length: N + 1 }, () => []); road.forEach(([from, to, cost]) => { graph[from].push([to, cost]); graph[to].push([from, cost]); }); const distance = new Array(N + 1).fill(Infinity); const minHeap = new MinHeap(); minHeap.heappush([0, start]); distance[start] = 0; while (minHeap.size) { const [nowDist, now] = minHeap.heappop(); if (distance[now] < nowDist) continue; for (const [nextCity, nextDist] of graph[now]) { let cost = nowDist + nextDist; if (cost < distance[nextCity]) { distance[nextCity] = cost; minHeap.heappush([cost, nextCity]); } } } return distance.filter((dist) => dist <= K).length; };
효성
첫 번째 풀이
플로이드 와샬로 time을 전부 구해버린 나쁜 풀이입니다..
이 문제는 n이 50으로 적은 편이라 플로이드 와샬로도 통과가 됐어요(feat. 갓재영)
function solution(N, road, K) { const board = new Array(N).fill().map(_ => new Array(N).fill(Infinity)); for(let i=0; i<N; i++) { board[i][i] = 0; } road.forEach((pos) => { const [x, y, time] = pos; board[x-1][y-1] = Math.min(board[x-1][y-1], time); board[y-1][x-1] = Math.min(board[y-1][x-1], time); }); for(let k = 0; k < N; k++) { for(let i = 0; i < N; i++) { for(let j = 0; j < N; j++) { if(board[i][j] > board[i][k] + board[k][j]) board[i][j] = board[i][k] + board[k][j]; } } } const start = board[0]; let answer = 0; for(let i=0; i<N; i++) { if(start[i] <= K) { answer++; } } return answer; }
두 번째 풀이
다익스트라로 좀 더 효율적이게!
function solution(N, road, K) { let arr = Array(N+1).fill(Infinity); let adj = Array.from(Array(N+1), () => Array()); for(let [a, b, c] of road) { adj[a].push({ to: b, time: c }); adj[b].push({ to: a, time: c }); } let check = [{ to: 1, time: 0 }]; arr[1] = 0; while(check.length) { let { to, time } = check.pop(); adj[to].forEach(next => { // next.to: 이동할 마을 / to: 현재 마을 / next.time: 이동할 마을까지 걸리는 시간 if(arr[next.to] > arr[to] + next.time) { arr[next.to] = arr[to] + next.time; check.push(next); } }); } return arr.filter((time) => time <= K).length; }
은찬
function solution(N, road, K) { let answer = 0; const graph = Array.from({length: N + 1}, () => Array()); const dist = Array(N + 1).fill(Infinity); const queue = []; for(let i = 0; i < road.length; i++){ const start = road[i][0]; const target = road[i][1]; const cost = road[i][2]; graph[start].push([target, cost]); graph[target].push([start, cost]); } queue.push([1, 0]); dist[1] = 0; while(queue.length){ const [current, cost] = queue.shift(); for(let i = 0; i < graph[current].length; i++){ const next = graph[current][i][0]; const nextCost = graph[current][i][1]; if(dist[next] > dist[current] + nextCost){ dist[next] = dist[current] + nextCost; queue.push([next, nextCost]); } } } for(let i = 1; i <= N; i++){ answer += dist[i] <= K ? 1 : 0; } return answer; }