๐น ๋ฑ์ฐ์ฝ์ค ์ ํ๊ธฐ
1. ๋ฌธ์ ๋งํฌ
2. ์ฝ๋
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
from collections import deque
INF = int(1e9)
def solution(n, paths, gates, summits):
graph = [[] for _ in range(n+1)]
summits.sort()
summits_set = set(summits)
gates = set(gates)
# ์ฐ๊ฒฐ ๋ด์ฉ ์ด๊ธฐํ
for i, j, w in paths:
graph[i].append((j, w))
graph[j].append((i, w))
intensity = [INF] * (n + 1)
result = [0, INF] # ์ฐ๋ด์ฐ๋ฆฌ, ์ต์ intensity
q = deque([])
for gate in gates:
q.append((0, gate))
intensity[gate] = 0
while q:
now_its, now = q.popleft()
# ์ฐ๋ด์ฐ๋ฆฌ ๋์ฐฉํ๊ฑฐ๋ ํ์ฌ ํ์ธํ๋ intensity๊ฐ ์ฐพ์ ์ต์๊ฐ๋ณด๋ค ํฌ๋ฉด ๋ ์ด์ ํ์ ์ํจ
if now in summits_set or now_its > intensity[now]:
continue
for next, next_its in graph[now]:
if intensity[next] > max(now_its, next_its):
intensity[next] = max(now_its, next_its)
q.append((intensity[next], next))
for summit in summits:
if result[1] > intensity[summit]:
result[1] = intensity[summit]
result[0] = summit
return result
ํ ์คํธ 1 ใ | ํต๊ณผ (0.02ms, 10.3MB) |
ํ ์คํธ 2 ใ | ํต๊ณผ (0.02ms, 10.1MB) |
ํ ์คํธ 3 ใ | ํต๊ณผ (0.01ms, 10.4MB) |
ํ ์คํธ 4 ใ | ํต๊ณผ (0.01ms, 10.2MB) |
ํ ์คํธ 5 ใ | ํต๊ณผ (0.02ms, 10.4MB) |
ํ ์คํธ 6 ใ | ํต๊ณผ (0.06ms, 10.4MB) |
ํ ์คํธ 7 ใ | ํต๊ณผ (0.07ms, 10.4MB) |
ํ ์คํธ 8 ใ | ํต๊ณผ (0.06ms, 10.4MB) |
ํ ์คํธ 9 ใ | ํต๊ณผ (0.12ms, 10.4MB) |
ํ ์คํธ 10 ใ | ํต๊ณผ (0.27ms, 10.3MB) |
ํ ์คํธ 11 ใ | ํต๊ณผ (0.20ms, 10.3MB) |
ํ ์คํธ 12 ใ | ํต๊ณผ (0.27ms, 10.2MB) |
ํ ์คํธ 13 ใ | ํต๊ณผ (6.58ms, 11.8MB) |
ํ ์คํธ 14 ใ | ํต๊ณผ (35.10ms, 20.1MB) |
ํ ์คํธ 15 ใ | ํต๊ณผ (275.07ms, 74.5MB) |
ํ ์คํธ 16 ใ | ํต๊ณผ (291.75ms, 77.2MB) |
ํ ์คํธ 17 ใ | ํต๊ณผ (302.45ms, 77.1MB) |
ํ ์คํธ 18 ใ | ํต๊ณผ (46.09ms, 15.5MB) |
ํ ์คํธ 19 ใ | ํต๊ณผ (195.38ms, 33.7MB) |
ํ ์คํธ 20 ใ | ํต๊ณผ (479.21ms, 71.2MB) |
ํ ์คํธ 21 ใ | ํต๊ณผ (310.52ms, 67.4MB) |
ํ ์คํธ 22 ใ | ํต๊ณผ (12.84ms, 14.9MB) |
ํ ์คํธ 23 ใ | ํต๊ณผ (134.82ms, 31.5MB) |
ํ ์คํธ 24 ใ | ํต๊ณผ (75.75ms, 27.1MB) |
ํ ์คํธ 25 ใ | ํต๊ณผ (477.51ms, 87.6MB) |
3. ํด์ค
summit๊น์ง ๋๋ฌํ๋ ๊ฒฝ์ฐ ์ค ์ต๋ intensity๊ฐ ๊ฐ์ฅ ์์ ๊ฐ์ ๋ฆฌํดํ๊ธฐ ์ํด์ intensity๋ฐฐ์ด(๊ฒฝ๋ก ์ค ์ต๋ intensity)๋ฅผ ์ด๊ธฐํํด์ค๋ค.
if intensity[next] > max(now_intensity, next_intensity)์ด๋ฉด intensity[next] = max(now_intensity, next_intensity)๋ก ์ด๊ธฐํํด์ค๋ค.
์ฒ์์๋ ๊ฐ ์ถ์ ๊ตฌ ๋ณ๋ก ๋ค์ต์คํธ๋ผ heapq๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์ ํ์ด๋ฅผ ์งํํ์ง๋ง ์๊ฐ์ด๊ณผ๊ฐ ๋ด๊ณ , ๋ชจ๋ ์ถ์ ๊ตฌ๋ฅผ heapq์ ๋ฃ์ ์ํ๋ก ์ํ๋ ๊ฐ๋ง ์ฐพ๋ ๋ฐฉ๋ฒ์ผ๋ก ์งํํ๋ค.
์ ๋ฐฉ์๋๋ก ๋ณ๊ฒฝํ ํ ๋ชจ๋ case๋ฅผ ํ์ธํด๋ณด๋ ๊ฒ์ด๋ผ ๊ตณ์ด ์ฐ์ ์์ ๋ฐฐ์ด์ ์ฌ์ฉํ์ฌ log(n)์ ์ถ๊ฐ ์๊ฐ๋ณต์ก๋๊ฐ ๋ฐ์ํ๋ heapq๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์์ด์ deque๋ฅผ ์ฌ์ฉํ๋ค.
summit๊ณผ gate์ ์ค๋ณต๋ ์์ ๊ธฐ ์ํด set()์ ์ฌ์ฉํ๋ค. summit์ ์ ๋ ฌ์ ํด ์ต์ ๊ฒฝ์ฐ๊ฐ ์ค๋ณต์ผ ๊ฒฝ์ฐ ์์๊ฐ์ ์ฌ์ฉํด์ฃผ์๋๋ฐ, set์ผ๋ก ๋ณ๊ฒฝํ๋ ์์ค ๋ญ๊ฐ ๋ฌธ์ ์ง.. ์์๊ฐ ๋ฐ์์ด ์๋ผ์ ํ์ธํ๋ ๊ฒฝ์ฐ๋ง ์ ๋ ฌํ list๋ฅผ ์ฌ์ฉํด์ฃผ์๋ค.