Post

๐Ÿน ๋“ฑ์‚ฐ์ฝ”์Šค ์ •ํ•˜๊ธฐ

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๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์—ˆ๋‹ค.

This post is licensed under CC BY 4.0 by the author.