forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.py
More file actions
38 lines (31 loc) Β· 1.23 KB
/
Copy path4.py
File metadata and controls
38 lines (31 loc) Β· 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
INF = int(1e9) # 무νμ μλ―Ένλ κ°μΌλ‘ 10μ΅μ μ€μ
# λ
Έλμ κ°μ λ° κ°μ μ κ°μλ₯Ό μ
λ ₯λ°κΈ°
n, m = map(int, input().split())
# 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κ° μλ‘μκ² κ°λ λΉμ©μ 1μ΄λΌκ³ μ€μ
a, b = map(int, input().split())
graph[a][b] = 1
graph[b][a] = 1
# κ±°μ³ κ° λ
Έλ Xμ μ΅μ’
λͺ©μ μ§ λ
Έλ Kλ₯Ό μ
λ ₯λ°κΈ°
x, k = map(int, input().split())
# μ νμμ λ°λΌ νλ‘μ΄λ μμ
μκ³ λ¦¬μ¦μ μν
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])
# μνλ κ²°κ³Όλ₯Ό μΆλ ₯
distance = graph[1][k] + graph[k][x]
# λλ¬ν μ μλ κ²½μ°, -1μ μΆλ ₯
if distance >= 1e9:
print("-1")
# λλ¬ν μ μλ€λ©΄, μ΅λ¨ 거리λ₯Ό μΆλ ₯
else:
print(distance)