문제 링크
https://www.acmicpc.net/problem/17396
n, m = map(int, input().split())
INF = int(1e9)
canSee = list(map(int, input().split()))
graph = [[] for _ in range(n)]
visited = [False] * (n)
distance = [INF] * (n)
for _ in range(m):
a, b, t = map(int, input().split())
if (canSee[a] == 0 or a == n-1) and (canSee[b] == 0 or b == n-1):
graph[a].append((b, t))
graph[b].append((a, t))
def get_smallest_node():
min_value = INF
index = 0
for i in range(n):
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 v in graph[start]:
distance[v[0]] = v[1]
for _ 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(0)
if distance[-1] == INF:
print(-1)
else:
print(distance[-1])
import heapq
n, m = map(int, input().split())
INF = int(1e9)
canSee = list(map(int, input().split()))
graph = [[] for _ in range(n)]
distance = [INF] * (n)
for _ in range(m):
a, b, t = map(int, input().split())
if (canSee[a] == 0 or a == n-1) and (canSee[b] == 0 or b == n-1):
graph[a].append((b, t))
graph[b].append((a, t))
def dijkstra(start):
q = []
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(0)
if distance[-1] == INF:
print(-1)
else:
print(distance[-1])
import heapq
import sys
input = sys.stdin.readline
inf = sys.maxsize
n, m = map(int, input().split())
canSee = list(map(int, input().split()))
canSee[-1] = 0
graph = [[] for _ in range(n)]
for _ in range(m):
a, b, t = map(int, input().split())
graph[a].append((b, t))
graph[b].append((a, t))
q = []
heapq.heappush(q, (0, 0))
distance = [inf] * n
distance[0] = 0
while q:
dist, now = heapq.heappop(q)
if distance[now] < dist:
continue
for nxt, nxt_cost in graph[now]:
cost = dist + nxt_cost
if cost < distance[nxt] and canSee[nxt] == 0:
distance[nxt] = cost
heapq.heappush(q, (cost, nxt))
print(distance[-1] if distance[-1]<inf else -1)
sys.stdin.readlind()
을 사용하자. 메모리와 실행시간을 절약한다.