Skip to content

Commit

Permalink
[코딩테스트책] 9-1. 미래도시 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
heerucan committed Mar 13, 2022
1 parent ef7f660 commit c8ea99c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions CodingTest/CH9 최단 경로/미래도시.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
n, m = map(int, input().split())
INF = int(1e9) # 무한을 의미하는 10억

# 2차원 리스트를 만들고, 모든 값을 무한으로 초기화
graph = [[INF]*(n+1) for _ in range(n+1)]

# 자기 자신 -> 자기 자신 가는 비용은 0으로 초기화
for a in range(1, n+1):
for b in range(1, n+1):
if a == b:
graph[a][b] = 0

# 각 간선에 대한 정보를 입력받아, 그 값으로 초기화
for _ in range(m):
# A -> B로 가는 비용은 C라고 설정
a, b = map(int, input().split())
graph[a][b] = 1
graph[b][a] = 1

# 점화식에 따라 플로이드 워셜 알고리즘을 수행
for k in range(1, n+1):
for a in range(1, n+1):
for b in range(1, n+1):
graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b])

x, k = map(int, input().split())

if graph[1][k] or graph[k][x] == INF:
print(-1)
else:
print(graph[1][k] + graph[k][x])


Empty file.

0 comments on commit c8ea99c

Please sign in to comment.