-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra1.py
More file actions
50 lines (43 loc) · 1.41 KB
/
dijkstra1.py
File metadata and controls
50 lines (43 loc) · 1.41 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
import sys
input = sys.stdin.readline
# 함수를 변수화, 간략화
INF = int(1e9)
# 무한 == 1억
n,m = tuple(map(int, input().split()))
#노드, 간선
start = int(input())
# 각 노드에 연결되어 있는 노드에 대한 정보를 담는 2차원 리스트 만들기
graph = [[] for i in range(n+1)]
visited = [False]*(n+1) # 방문 노드 리스트
distance = [INF]*(n+1) # 노드 최단거리 리스트
for _ in range(m):
a,b,c = map(int, input().split())
graph[a].append((b,c)) # a를 시작으로하는 노드의 도착점과 길이
def get_smallest_node(): # 방문하지 않은 노드 중, 최단거리 노드 반환
min_value = INF
index = 0
for i in range(1, n+1):
if distance[i] < min_value and not visited[i]:
min_value = distance[i]
index = i
return index
def dijkstra(start):
distance[start] = 0
visited[start] = True
for j in graph[start]:
distance[j[0]] = j[1]
# 도착점, 거리
for i in range(n-1):
now = get_smallest_node()
visited[now] = True
for j in graph[now]:
cost = distance[now] + j[1]
if cost < distance[j[0]]:
distance[j[0]] = cost
dijkstra(start)
# 모든 노드로 가는 최단거리
for i in range(1,n+1):
if distance[i]==INF:
print("NOT REACH")
else:
print(distance[i])