forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path5.py
More file actions
51 lines (45 loc) Β· 1.79 KB
/
5.py
File metadata and controls
51 lines (45 loc) Β· 1.79 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
39
40
41
42
43
44
45
46
47
48
49
50
51
import heapq
import sys
input = sys.stdin.readline
INF = int(1e9) # 무νμ μλ―Ένλ κ°μΌλ‘ 10μ΅μ μ€μ
# λ
Έλμ κ°μ, κ°μ μ κ°μ, μμ λ
Έλλ₯Ό μ
λ ₯λ°κΈ°
n, m, start = map(int, input().split())
# κ° λ
Έλμ μ°κ²°λμ΄ μλ λ
Έλμ λν μ 보λ₯Ό λ΄λ 리μ€νΈλ₯Ό λ§λ€κΈ°
graph = [[] for i in range(n + 1)]
# μ΅λ¨ 거리 ν
μ΄λΈμ λͺ¨λ 무νμΌλ‘ μ΄κΈ°ν
distance = [INF] * (n + 1)
# λͺ¨λ κ°μ μ 보λ₯Ό μ
λ ₯λ°κΈ°
for _ in range(m):
x, y, z = map(int, input().split())
# Xλ² λ
Έλμμ Yλ² λ
Έλλ‘ κ°λ λΉμ©μ΄ ZλΌλ μλ―Έ
graph[x].append((y, z))
def dijkstra(start):
q = []
# μμ λ
Έλλ‘ κ°κΈ° μν μ΅λ¨ κ²½λ‘λ 0μΌλ‘ μ€μ νμ¬, νμ μ½μ
heapq.heappush(q, (0, start))
distance[start] = 0
while q: # νκ° λΉμ΄μμ§ μλ€λ©΄
# κ°μ₯ μ΅λ¨ κ±°λ¦¬κ° μ§§μ λ
Έλμ λν μ 보λ₯Ό κΊΌλ΄κΈ°
dist, now = heapq.heappop(q)
if distance[now] < dist:
continue
# νμ¬ λ
Έλμ μ°κ²°λ λ€λ₯Έ μΈμ ν λ
Έλλ€μ νμΈ
for i in graph[now]:
cost = dist + i[1]
# νμ¬ λ
Έλλ₯Ό κ±°μ³μ, λ€λ₯Έ λ
Έλλ‘ μ΄λνλ κ±°λ¦¬κ° λ μ§§μ κ²½μ°
if cost < distance[i[0]]:
distance[i[0]] = cost
heapq.heappush(q, (cost, i[0]))
# λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μν
dijkstra(start)
# λλ¬ν μ μλ λ
Έλμ κ°μ
count = 0
# λλ¬ν μ μλ λ
Έλ μ€μμ, κ°μ₯ λ©λ¦¬ μλ λ
Έλμμ μ΅λ¨ 거리
max_distance = 0
for d in distance:
# λλ¬ν μ μλ λ
ΈλμΈ κ²½μ°
if d != 1e9:
count += 1
max_distance = max(max_distance, d)
# μμ λ
Έλλ μ μΈν΄μΌ νλ―λ‘ count - 1μ μΆλ ₯
print(count - 1, max_distance)