https://www.acmicpc.net/problem/17396
17396번: 백도어
첫 번째 줄에 분기점의 수와 분기점들을 잇는 길의 수를 의미하는 두 자연수 N과 M이 공백으로 구분되어 주어진다.(1 ≤ N ≤ 100,000, 1 ≤ M ≤ 300,000) 두 번째 줄에 각 분기점이 적의 시야에 보이는
www.acmicpc.net
평범한 다익스트라 문제에 방문하지 못하는 정점의 조건을 더한 문제이다.
중간에 현재 방문한 정점과 연결된 정점이 방문할 수 있는 정점인지를 확인하는 조건문을 추가했다.
import sys, heapq
N, M = map(int, input().split())
check = list(map(int, input().split()))
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])
ans = [float('INF')] * N
def solve(start):
ans[start] = 0
h = []
heapq.heappush(h, [ans[start], start])
while(h):
now_dist, now_visit = heapq.heappop(h)
if (ans[now_visit] < now_dist):
continue
for v, d in graph[now_visit]:
if (check[v]==1 and v!=(N-1)):
continue
dist = now_dist + d
if (dist < ans[v]):
ans[v] = dist
heapq.heappush(h, [dist, v])
solve(0)
if (ans[N-1]==float('INF')): print(-1)
else: print(ans[N-1])
'알고리즘💻 > 우선순위큐&다익스트라' 카테고리의 다른 글
BOJ 18223번: 민준이와 마산 그리고 건우 (0) | 2021.08.21 |
---|---|
BOJ 1655번: 가운데를 말해요 (0) | 2021.08.19 |
BOJ 1504번: 특정한 최단 경로 (0) | 2021.08.19 |
BOJ 22116번: 창영이와 퇴근 (0) | 2021.08.17 |
BOJ 11279번: 최대 힙 (0) | 2021.02.15 |