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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
๋“ฑ์‚ฐ์ฝ”์Šค ์ •ํ•˜๊ธฐ

๐Ÿ’› ๋ฌธ์ œ
XX์‚ฐ์€ n๊ฐœ์˜ ์ง€์ ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์ง€์ ์€ 1๋ถ€ํ„ฐ n๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด์žˆ์œผ๋ฉฐ,
์ถœ์ž…๊ตฌ, ์‰ผํ„ฐ, ํ˜น์€ ์‚ฐ๋ด‰์šฐ๋ฆฌ์ž…๋‹ˆ๋‹ค.
๊ฐ ์ง€์ ์€ ์–‘๋ฐฉํ–ฅ ํ†ตํ–‰์ด ๊ฐ€๋Šฅํ•œ ๋“ฑ์‚ฐ๋กœ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉฐ, ์„œ๋กœ ๋‹ค๋ฅธ ์ง€์ ์„ ์ด๋™ํ•  ๋•Œ
์ด ๋“ฑ์‚ฐ๋กœ๋ฅผ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๋“ฑ์‚ฐ๋กœ๋ณ„๋กœ ์ด๋™ํ•˜๋Š”๋ฐ ์ผ์ • ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค.

๋“ฑ์‚ฐ์ฝ”์Šค๋Š” ๋ฐฉ๋ฌธํ•  ์ง€์  ๋ฒˆํ˜ธ๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•˜์—ฌ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด 1-2-3-2-1 ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋“ฑ์‚ฐ์ฝ”์Šค๋Š” 1๋ฒˆ์ง€์ ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ 2๋ฒˆ, 3๋ฒˆ, 2๋ฒˆ, 1๋ฒˆ ์ง€์ ์„ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.
๋“ฑ์‚ฐ์ฝ”์Šค๋ฅผ ๋”ฐ๋ผ ์ด๋™ํ•˜๋Š” ์ค‘ ์‰ผํ„ฐ ํ˜น์€ ์‚ฐ๋ด‰์šฐ๋ฆฌ๋ฅผ ๋ฐฉ๋ฌธํ•  ๋•Œ๋งˆ๋‹ค ํœด์‹์„ ์ทจํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ,
ํœด์‹ ์—†์ด ์ด๋™ํ•ด์•ผ ํ•˜๋Š” ์‹œ๊ฐ„ ์ค‘ ๊ฐ€์žฅ ๊ธด ์‹œ๊ฐ„์„ ํ•ด๋‹น ๋“ฑ์‚ฐ์ฝ”์Šค์˜ intensity๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋กœ ํ•ฉ๋‹ˆ๋‹ค.

๋‹น์‹ ์€ XX์‚ฐ์˜ ์ถœ์ž…๊ตฌ ์ค‘ ํ•œ ๊ณณ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ์‚ฐ๋ด‰์šฐ๋ฆฌ ์ค‘ ํ•œ ๊ณณ๋งŒ ๋ฐฉ๋ฌธํ•œ ๋’ค
๋‹ค์‹œ ์›๋ž˜์˜ ์ถœ์ž…๊ตฌ๋กœ ๋Œ์•„์˜ค๋Š” ๋“ฑ์‚ฐ์ฝ”์Šค๋ฅผ ์ •ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
๋‹ค์‹œ ๋งํ•ด, ๋“ฑ์‚ฐ์ฝ”์Šค์—์„œ ์ถœ์ž…๊ตฌ๋Š” ์ฒ˜์Œ๊ณผ ๋์— ํ•œ ๋ฒˆ์”ฉ, ์‚ฐ๋ด‰์šฐ๋ฆฌ๋Š” ํ•œ ๋ฒˆ๋งŒ ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋‹น์‹ ์€ ์ด๋Ÿฌํ•œ ๊ทœ์น™์„ ์ง€ํ‚ค๋ฉด์„œ intensity๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก ๋“ฑ์‚ฐ์ฝ”์Šค๋ฅผ ์ •ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

XX์‚ฐ์˜ ์ง€์  ์ˆ˜ n, ๊ฐ ๋“ฑ์‚ฐ๋กœ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด paths,
์ถœ์ž…๊ตฌ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ์ •์ˆ˜ ๋ฐฐ์—ด gates, ์‚ฐ๋ด‰์šฐ๋ฆฌ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ์ •์ˆ˜ ๋ฐฐ์—ด summits๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
์ด๋•Œ, intensity๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๋“ฑ์‚ฐ์ฝ”์Šค์— ํฌํ•จ๋œ ์‚ฐ๋ด‰์šฐ๋ฆฌ ๋ฒˆํ˜ธ์™€ intensity์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ
์ •์ˆ˜ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
intensity๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๋“ฑ์‚ฐ์ฝ”์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๋ฉด ๊ทธ์ค‘ ์‚ฐ๋ด‰์šฐ๋ฆฌ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ๋“ฑ์‚ฐ์ฝ”์Šค๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿงก ์ œํ•œ ์‚ฌํ•ญ
2 โ‰ค n โ‰ค 50,000
n - 1 โ‰ค paths์˜ ๊ธธ์ด โ‰ค 200,000
paths์˜ ์›์†Œ๋Š” [i, j, w] ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค.
    i๋ฒˆ ์ง€์ ๊ณผ j๋ฒˆ ์ง€์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋“ฑ์‚ฐ๋กœ๊ฐ€ ์žˆ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.
    w๋Š” ๋‘ ์ง€์  ์‚ฌ์ด๋ฅผ ์ด๋™ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.
    1 โ‰ค i < j โ‰ค n
    1 โ‰ค w โ‰ค 10,000,000
    ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์ง€์ ์„ ์ง์ ‘ ์—ฐ๊ฒฐํ•˜๋Š” ๋“ฑ์‚ฐ๋กœ๋Š” ์ตœ๋Œ€ 1๊ฐœ์ž…๋‹ˆ๋‹ค.
1 โ‰ค gates์˜ ๊ธธ์ด โ‰ค n
    1 โ‰ค gates์˜ ์›์†Œ โ‰ค n
    gates์˜ ์›์†Œ๋Š” ํ•ด๋‹น ์ง€์ ์ด ์ถœ์ž…๊ตฌ์ž„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
1 โ‰ค summits์˜ ๊ธธ์ด โ‰ค n
    1 โ‰ค summits์˜ ์›์†Œ โ‰ค n
    summits์˜ ์›์†Œ๋Š” ํ•ด๋‹น ์ง€์ ์ด ์‚ฐ๋ด‰์šฐ๋ฆฌ์ž„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
์ถœ์ž…๊ตฌ์ด๋ฉด์„œ ๋™์‹œ์— ์‚ฐ๋ด‰์šฐ๋ฆฌ์ธ ์ง€์ ์€ ์—†์Šต๋‹ˆ๋‹ค.
gates์™€ summits์— ๋“ฑ์žฅํ•˜์ง€ ์•Š์€ ์ง€์ ์€ ๋ชจ๋‘ ์‰ผํ„ฐ์ž…๋‹ˆ๋‹ค.
์ž„์˜์˜ ๋‘ ์ง€์  ์‚ฌ์ด์— ์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๊ฐ€ ํ•ญ์ƒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
return ํ•˜๋Š” ๋ฐฐ์—ด์€ [์‚ฐ๋ด‰์šฐ๋ฆฌ์˜ ๋ฒˆํ˜ธ, intensity์˜ ์ตœ์†Ÿ๊ฐ’] ์ˆœ์„œ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’š ์ž…์ถœ๋ ฅ
n	paths	                                                                                    gates	summits	    result
6	[[1, 2, 3], [2, 3, 5], [2, 4, 2], [2, 5, 4], [3, 4, 4], [4, 5, 3], [4, 6, 1], [5, 6, 1]]	[1, 3]	[5]	        [5, 3]
7	[[1, 4, 4], [1, 6, 1], [1, 7, 3], [2, 5, 2], [3, 7, 4], [5, 6, 6]]	                        [1]	[2, 3, 4]	    [3, 4]
7	[[1, 2, 5], [1, 4, 1], [2, 3, 1], [2, 6, 7], [4, 5, 1], [5, 6, 1], [6, 7, 1]]	            [3, 7]	[1, 5]	    [5, 1]
5	[[1, 3, 10], [1, 4, 20], [2, 3, 4], [2, 4, 6], [3, 5, 20], [4, 5, 6]]	                    [1, 2]	[5]	        [5, 6]
"""

# XX์‚ฐ์˜ ์ง€์  ์ˆ˜ n, ๊ฐ ๋“ฑ์‚ฐ๋กœ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด paths,
# ์ถœ์ž…๊ตฌ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ์ •์ˆ˜ ๋ฐฐ์—ด gates, ์‚ฐ๋ด‰์šฐ๋ฆฌ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ์ •์ˆ˜ ๋ฐฐ์—ด summits
import heapq

def solution(n, paths, gates, summits):
    graph = [[] for _ in range(n + 1)]

    # i๋ฒˆ ์ง€์ ๊ณผ j๋ฒˆ ์ง€์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋“ฑ์‚ฐ๋กœ๊ฐ€ ์žˆ๋‹ค
    # w๋Š” ๋‘ ์ง€์  ์‚ฌ์ด๋ฅผ ์ด๋™ํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
    for i, j, w in paths:
        graph[i].append([j, w])
        graph[j].append([i, w])

    visited = [False] * (n + 1)

    for summit in summits:
        visited[summit] = True

    max_int = 1e9
    distance = [max_int] * (n + 1)

    temp = []
    # ์ถœ์ž…๊ตฌ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ์ •์ˆ˜ ๋ฐฐ์—ด
    for gate in gates:
        distance[gate] = 0
        heapq.heappush(temp, [0, gate])

    # ํœด์‹ ์—†์ด ์ด๋™ํ•ด์•ผ ํ•˜๋Š” ์‹œ๊ฐ„ ์ค‘ ๊ฐ€์žฅ ๊ธด ์‹œ๊ฐ„์„ ํ•ด๋‹น ๋“ฑ์‚ฐ์ฝ”์Šค์˜ intensit
    # ๋“ฑ์‚ฐ์ฝ”์Šค์—์„œ ์ถœ์ž…๊ตฌ๋Š” ์ฒ˜์Œ๊ณผ ๋์— ํ•œ ๋ฒˆ์”ฉ, ์‚ฐ๋ด‰์šฐ๋ฆฌ๋Š” ํ•œ ๋ฒˆ๋งŒ ํฌํ•จ
    # intensity๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก ๋“ฑ์‚ฐ์ฝ”์Šค๋ฅผ ์ •ํ•˜๊ธฐ

    while temp:
        d, i = heapq.heappop(temp)

        # ์‚ฐ๋ด‰์šฐ๋ฆฌ๋ฉด continue
        if distance[i] < d or visited[i]:
            continue
        for j, dd in graph[i]:
            dd = max(distance[i], dd)
            if distance[j] > dd:
                distance[j] = dd
                heapq.heappush(temp, [dd, j])

    result = [-1, max_int]
    for summit in sorted(summits):
        if distance[summit] < result[1]:
            result[0] = summit
            result[1] = distance[summit]

    # [์‚ฐ๋ด‰์šฐ๋ฆฌ์˜ ๋ฒˆํ˜ธ, intensity์˜ ์ตœ์†Ÿ๊ฐ’]
    return result
  • ์ •ํ™•์„ฑ

    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
    
      ํ…Œ์ŠคํŠธ 1 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.1MB)
      ํ…Œ์ŠคํŠธ 2 ใ€‰ ํ†ต๊ณผ (0.05ms, 10.5MB)
      ํ…Œ์ŠคํŠธ 3 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.1MB)
      ํ…Œ์ŠคํŠธ 4 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 5 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 6 ใ€‰ ํ†ต๊ณผ (0.10ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 7 ใ€‰ ํ†ต๊ณผ (0.15ms, 10.1MB)
      ํ…Œ์ŠคํŠธ 8 ใ€‰ ํ†ต๊ณผ (0.08ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 9 ใ€‰ ํ†ต๊ณผ (0.25ms, 10.3MB)
      ํ…Œ์ŠคํŠธ 10 ใ€‰ ํ†ต๊ณผ (0.31ms, 10.4MB)
      ํ…Œ์ŠคํŠธ 11 ใ€‰ ํ†ต๊ณผ (0.27ms, 10.1MB)
      ํ…Œ์ŠคํŠธ 12 ใ€‰ ํ†ต๊ณผ (0.35ms, 10.2MB)
      ํ…Œ์ŠคํŠธ 13 ใ€‰ ํ†ต๊ณผ (6.70ms, 11.7MB)
      ํ…Œ์ŠคํŠธ 14 ใ€‰ ํ†ต๊ณผ (70.21ms, 20.9MB)
      ํ…Œ์ŠคํŠธ 15 ใ€‰ ํ†ต๊ณผ (675.26ms, 80.7MB)
      ํ…Œ์ŠคํŠธ 16 ใ€‰ ํ†ต๊ณผ (768.15ms, 83.1MB)
      ํ…Œ์ŠคํŠธ 17 ใ€‰ ํ†ต๊ณผ (691.59ms, 83.3MB)
      ํ…Œ์ŠคํŠธ 18 ใ€‰ ํ†ต๊ณผ (29.58ms, 16MB)
      ํ…Œ์ŠคํŠธ 19 ใ€‰ ํ†ต๊ณผ (266.96ms, 36.4MB)
      ํ…Œ์ŠคํŠธ 20 ใ€‰ ํ†ต๊ณผ (691.40ms, 77.6MB)
      ํ…Œ์ŠคํŠธ 21 ใ€‰ ํ†ต๊ณผ (962.79ms, 73.3MB)
      ํ…Œ์ŠคํŠธ 22 ใ€‰ ํ†ต๊ณผ (39.48ms, 15.2MB)
      ํ…Œ์ŠคํŠธ 23 ใ€‰ ํ†ต๊ณผ (226.51ms, 32.5MB)
      ํ…Œ์ŠคํŠธ 24 ใ€‰ ํ†ต๊ณผ (117.11ms, 27.6MB)
      ํ…Œ์ŠคํŠธ 25 ใ€‰ ํ†ต๊ณผ (1184.18ms, 95.1MB)
    


3. ํ•ด์„ค

1
2
3
4
5
- ๊ทธ๋ž˜ํ”„๋ฅผ ์ด์šฉํ•จ
- ๊ฐ ์œ„์น˜์— ๋Œ€ํ•ด ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ๊ฐ€์ค‘์น˜๋ฅผ ๊ตฌํ•ด์•ผํ•จ
    - ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ํ†ตํ•ด max ์—ฐ์‚ฐ์œผ๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค
- ๋‘ ๊ฐœ ์ด์ƒ ์‚ฐ๋ด‰์šฐ๋ฆฌ ๋ฐฉ๋ฌธ X
    - ์‚ฐ๋ด‰์šฐ๋ฆฌ ์ฒดํฌ๋ฅผ ์ž˜ ํ•˜๋„๋ก!
This post is licensed under CC BY 4.0 by the author.