문제
코딩테스트 연습 - 배달
5 [[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]] 3 4 6 [[1,2,1],[1,3,2],[2,3,2],[3,4,3],[3,5,2],[3,5,3],[5,6,1]] 4 4
programmers.co.kr
알고리즘
- bfs 탐색을 통해 문제를 수행한다.
코드
# 23:15
import sys
from collections import deque
def solution(N, road, K):
answer = 0
graph = [[] for _ in range(N + 1)]
visited = [sys.maxsize] * (N + 1)
visited[1] = 0
for a, b, c in road:
graph[a].append([b, c])
graph[b].append([a, c])
queue = deque([[1, 0]])
# bfs
while queue:
target, num = queue.popleft()
for j in graph[target]:
if num + j[1] <= K and num + j[1] < visited[j[0]]:
visited[j[0]] = num + j[1]
queue.append([j[0], num + j[1]])
for k in visited:
if k != sys.maxsize:
answer += 1
return answer
github
GitHub - junjange/CodingTest: 내가 푼 코딩 테스트 문제와 해결법
내가 푼 코딩 테스트 문제와 해결법. Contribute to junjange/CodingTest development by creating an account on GitHub.
github.com
'CodingTest > Programers' 카테고리의 다른 글
[programers] 프로그래머스(파이썬) : 점프와 순간 이동 (0) | 2022.04.25 |
---|---|
[programers] 프로그래머스(파이썬) : 영어 끝말잇기 (0) | 2022.04.24 |
[programers] 프로그래머스(파이썬) : 신규 아이디 추천 (0) | 2022.04.24 |
[programers] 프로그래머스(파이썬) : 로또의 최고 순위와 최저 순위 (0) | 2022.04.24 |
[programers] 프로그래머스(파이썬) : 소수 만들기 (0) | 2022.04.24 |