๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 11952๋ฒ
ํ์ด ๊ณผ์
๋ฌธ์ ์์ ์ฃผ์ด์ง ์ข๋น๋ก ์ ๋ น๋ ๋์๋ค์ ์ด์ฉํด์ ์ํํ ์ง์ญ์ ์ฐพ๊ณ ,
์ด๋ฅผ ํตํด ๋์ ๋ฐฉ๋ฌธ ๋น์ฉ์ ๊ณ์ฐํ๊ณ ๋ค์ต์คํธ๋ผ
์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํฉ๋๋ค.
์ด๋ ๋์์๊ฒ ์ ๋ น๋นํ ๋์๋ ์ต๋จ ๊ฑฐ๋ฆฌ ๊ณ์ฐ ์ ๊ณ ๋ คํ์ง ์๋๋ก ํ๋ ๊ฒ์ ์ ์ํฉ๋๋ค.
์ข๋น์๊ฒ ์ ๋ น๋นํ ๋์๋ฅผ ๊ธฐ์ค์ผ๋ก S
์ดํ์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ์ง๋ ์ํํ ๋์๋ฅผ ํ๋จํ๊ธฐ ์ํด์๋ BFS
๋ฅผ ์ด์ฉํ์ต๋๋ค.
์ฝ๋
import sys
from collections import deque
import heapq
N, M, K, S = list(map(int, sys.stdin.readline().split()))
P, Q = list(map(int, sys.stdin.readline().split()))
occupied = [int(sys.stdin.readline().strip()) for _ in range(K)]
edges = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]
def make_graph():
graph = [[] for _ in range(N + 1)]
for frm, to in edges:
graph[frm].append(to)
graph[to].append(frm)
return graph
def find_zombie_land(graph):
zombie_land = [0] * (N + 1)
for start in occupied:
lands = bfs(start, graph, S)
for land in lands:
zombie_land[land] = 1
zombie_land[1] = 0
zombie_land[N] = 0
return zombie_land
def bfs(start, graph, offset):
visit = [0] * (N + 1)
q = deque()
ret = []
visit[start] = 1
q.append([start, 0])
while q:
here, move = q.popleft()
if move > offset:
continue
else:
ret.append(here)
for there in graph[here]:
if not visit[there]:
visit[there] = 1
q.append([there, move + 1])
return ret
def dijkstra(graph, zombie_land):
pq = []
dist = [sys.maxsize] * (N + 1)
dist[1] = 0
heapq.heappush(pq, [0, 1])
while pq:
cost, here = heapq.heappop(pq)
if dist[here] < cost:
continue
for there in graph[here]:
if there in occupied:
continue
next_cost = cost + (Q if zombie_land[there] else P)
if next_cost < dist[there]:
dist[there] = next_cost
heapq.heappush(pq, [next_cost, there])
return dist[N] - P
def solution():
graph = make_graph()
zombie_land = find_zombie_land(graph)
answer = dijkstra(graph, zombie_land)
return answer
print(solution())
๋ฐ์ํ
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 6156 - Cow Contest (0) | 2021.03.08 |
---|---|
BOJ 9625 - BABBA (0) | 2021.03.08 |
BOJ 20055 - ์ปจ๋ฒ ์ด์ด ๋ฒจํธ ์์ ๋ก๋ด (0) | 2021.03.08 |
BOJ 20044 - Project Teams (0) | 2021.03.08 |
BOJ 1644 - ์์์ ์ฐ์ํฉ (0) | 2021.03.08 |
๐ฌ ๋๊ธ