[Programmers] 배달

Updated:

배달

배달 을 ν΄λ¦­ν•˜λ©΄ λ°”λ‘œ μ΄λ™ν•œλ‹€.

1번 λ…Έλ“œμ—μ„œ K μ΄ν•˜μ˜ 거리λ₯Ό 가진 λ…Έλ“œκ°€ λͺ‡ κ°œμΈμ§€ κ΅¬ν•˜λ©΄ λ˜λŠ” λ¬Έμ œμ΄λ‹€.

전체 λ…Έλ“œλŠ” μ΅œλŒ€ 50개 μ΄λ―€λ‘œ λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•œλ‹€.

λ‹€λ§Œ λ…Έλ“œ μ‚¬μ΄μ˜ 간선이 μ—¬λŸ¬ 개 μžˆμ„ 수 있기 λ•Œλ¬Έμ— 인접행렬을 λ§Œλ“œλŠ” κ³Όμ •μ—μ„œ 제일 μž‘μ€ 거리λ₯Ό κ°€μ§€λŠ” κ°„μ„ λ§Œ λ„£μ–΄μ€€λ‹€.


def solution(N, road, K):
	answer = 0
	INF = float("INF")
	graph = [[INF] * N for _ in range(N)]

	for i in road:
		start, end, time = i
		if graph[start - 1][end - 1] > time:
			graph[start - 1][end - 1] = time
			graph[end - 1][start - 1] = time

	for k in range(N):
		for i in range(N):
			for j in range(N):
				if i != j:
					graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
				else:
					graph[i][j] = 0

	for i in graph[0]:
		if i <= K:
			answer += 1

	return answer


if __name__ == '__main__':
	N = 5
	road = [[1, 2, 1], [2, 3, 3], [5, 2, 2], [1, 4, 2], [5, 3, 1], [5, 4, 2]]
	K = 3

	print(solution(N, road, K))



Categories:

Updated:

Leave a comment