Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26-seongwon030 #96

Merged
merged 1 commit into from
Sep 2, 2024
Merged

26-seongwon030 #96

merged 1 commit into from
Sep 2, 2024

Conversation

seongwon030
Copy link
Collaborator

@seongwon030 seongwon030 commented Aug 21, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

3์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์— ๊ฒฝ๋กœ๋ฅผ ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ์ตœ์†Œ๋น„์šฉ์„ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ž‘๋™๊ณผ์ •์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1. ์ถœ๋ฐœ ๋…ธ๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
2. ์ถœ๋ฐœ ๋…ธ๋“œ ๊ธฐ์ค€์œผ๋กœ ๊ฐ ๋…ธ๋“œ์˜ ์ตœ์†Œ ๋น„์šฉ์„ ์ €์žฅํ•œ๋‹ค.
3. ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘ ๊ฐ€์žฅ ๋น„์šฉ์ด ์ ์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•œ๋‹ค.
4. ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ ํŠน์ •ํ•œ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•ด ์ตœ์†Œ ๋น„์šฉ์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
5. 3๋ฒˆ๊ณผ 4๋ฒˆ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์ •์  ๋‘ ๊ฐœ v1,v2๋ฅผ ๋ฌด์กฐ๊ฑด ์ง€๋‚˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋Ÿผ ๊ฒฝ๋กœ๋ฅผ ์ด ์„ธ๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ๋Š” ์ถœ๋ฐœ ๋…ธ๋“œ์—์„œ v1/v2๊นŒ์ง€ v1/v2์—์„œ v1/v2 ๊นŒ์ง€ v1/v2์—์„œ ๋„์ฐฉ ๋…ธ๋“œ๊นŒ์ง€์ž…๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ /๋ฅผ ํ•œ ๊ฑด v1์—์„œ v2๋กœ ๊ฐˆ ์ˆ˜๋„ ์žˆ๊ณ v2์—์„œ v1์œผ๋กœ ๊ฐˆ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋‹ค์ต์ŠคํŠธ๋ผ ๊ตฌํ˜„

def di(start,size):
    # ์‚ฌ์šฉํ•œ ๊ธธ ๊ณ„์† ์‚ฌ์šฉ ๊ฐ€๋Šฅ ํ•˜๋ฏ€๋กœ ๊ณ„์† ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
    distance = [float('inf')] * (size+1)
    q = []
    heapq.heappush(q, [0,start])
    distance[start] = 0
    
    while q:
        dist, node = heapq.heappop(q)
        
        if distance[node] < dist: #  ๊ธฐ์กด์— ์žˆ๋˜ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ๊ธธ๋ฉด ๋„˜์–ด๊ฐ€๊ธฐ
            continue
        for n,w in graph[node]:
            cost = dist + w # ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ฑฐ์น  ๋–„์˜ ๊ฑฐ๋ฆฌ ๋น„์šฉ
            
            if distance[n] > cost:  # ์•Œ๊ณ  ์žˆ๋Š” ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ฐฑ์‹ 
                distance[n]= cost 
                heapq.heappush(q,[cost,n]) # ํ์— ์‚ฝ์ž…ํ•˜์—ฌ ๋‹ค์‹œ ๋ฐ˜๋ณต
                
    return distance
  • ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐํ•˜๋ฉด์„œ ํ˜„์žฌ ์•„๋Š” ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์งง์œผ๋ฉด ํ•ด๋‹น ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ 
  • ๊ฐฑ์‹ ๋œ ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ํ์— ๋„ฃ์–ด ์ง„ํ–‰

๊ฒฐ๊ณผ

#๊ฐ๊ฐ 1,v1,v2๊ฐ€ ์‹œ์ž‘์ ์œผ๋กœ ๊ฐ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.
d1 = di(1,n)
d2 = di(v1,n)
d3 = di(v2,n)
# v1์„ ๋จผ์ € ๋“ค๋ฆฌ๊ฑฐ๋‚˜ v2๋ฅผ ๋จผ์ € ๋“ค๋ฆฌ๊ฑฐ๋‚˜ ๋น„๊ตํ•ด์„œ ๋” ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
result = min(d1[v2]+d2[n]+d3[v1],d1[v1]+d2[v2]+d3[n])

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ž˜ ์•ˆ ํ’€๋ ค์„œ ๋ธ”๋กœ๊ทธ ์ฐธ๊ณ ํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.. ๋‹ค์ต์ŠคํŠธ๋ผ ์‹œ์ž‘์ž…๋‹ˆ๋‹ค

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์‰ฝ์ง€ ์•Š์€ ๋ฌธ์ œ๊ตฐ์š”...

์ฒ˜์Œ์—๋Š” DFS๋กœ ํ•„์ˆ˜ ๋ฐฉ๋ฌธ๋…ธ๋“œ๋ฅผ ์ฒดํฌํ•˜๋ฉด์„œ ํ•ด๋ณผ๊นŒ๋„ ํ–ˆ์ง€๋งŒ N๊ณผ E์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ปค์„œ ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚  ๊ฒƒ ๊ฐ™์•„์„œ ํฌ๊ธฐ
๋ฐ”๋กœ MST๊นŒ์ง€ ๋– ์˜ฌ๋ž์ง€๋งŒ ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ–ˆ์„ ๋•Œ ๋งŒ์•ฝ ์ฃผ์–ด์ง„ ๊ฐ„์„ ๋“ค์ด ์šฐํšŒํ•˜๊ฑฐ๋‚˜ ๊บพ์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ
2 -> 5 : 10
2 -> 7 -> 5 : 8
์™€ ๊ฐ™์€ ์ผ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋ฉด ์ด์ „์— ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ฒฝํ—˜์ด ์žˆ์„ ๊ฒฝ์šฐ ์ตœ์†Œ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•˜๊ธฐ ์–ด๋ ต๋”๋ผ๊ตฌ์šฉ
ํ”„๋ฆผ์œผ๋กœ ํ’€์–ด๋ณผ๊นŒ ํ–ˆ์ง€๋งŒ ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์‹œ์ž‘์ ์ด ์ •ํ•ด์ ธ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ์ข€ ๋” ๋ฐ”๋žŒ์งํ•˜๋‹ค๋Š” ํŒ๋‹จ.
๊ทธ๋ž˜์„œ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ ์ค‘๊ฐ„์— ํ•„์ˆ˜ ๋ฐฉ๋ฌธ๊นŒ์ง€ ์ฒดํฌํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ 1 ์—์„œ v1, v2๊นŒ์ง€ ๊ทธ๋ฆฌ๊ณ  v1, v2์—์„œ N ๊นŒ์ง€ ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ๋– ์˜ฌ๋ ธ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ v1์—์„œ v2 ํ˜น์€ v2์—์„œ v1์œผ๋กœ ๊ฐ€๋Š” ๊ฒƒ์„ ์ฒดํฌ์•ˆํ•ด์ค˜์„œ ๋˜ ์˜ˆ์™ธ์‚ฌํ•ญ ๋ฐœ๊ฒฌํ•˜๋Š”๋ฐ ํ•˜๋ฃจ๊ฐ€ ๊ฑธ๋ ธ๋„ค์š”

๋ฌธ์ œ ์ž˜ ๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค

@seongwon030
Copy link
Collaborator Author

์‰ฝ์ง€ ์•Š์€ ๋ฌธ์ œ๊ตฐ์š”...

์ฒ˜์Œ์—๋Š” DFS๋กœ ํ•„์ˆ˜ ๋ฐฉ๋ฌธ๋…ธ๋“œ๋ฅผ ์ฒดํฌํ•˜๋ฉด์„œ ํ•ด๋ณผ๊นŒ๋„ ํ–ˆ์ง€๋งŒ N๊ณผ E์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ปค์„œ ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚  ๊ฒƒ ๊ฐ™์•„์„œ ํฌ๊ธฐ ๋ฐ”๋กœ MST๊นŒ์ง€ ๋– ์˜ฌ๋ž์ง€๋งŒ ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ–ˆ์„ ๋•Œ ๋งŒ์•ฝ ์ฃผ์–ด์ง„ ๊ฐ„์„ ๋“ค์ด ์šฐํšŒํ•˜๊ฑฐ๋‚˜ ๊บพ์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ 2 -> 5 : 10 2 -> 7 -> 5 : 8 ์™€ ๊ฐ™์€ ์ผ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋ฉด ์ด์ „์— ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ฒฝํ—˜์ด ์žˆ์„ ๊ฒฝ์šฐ ์ตœ์†Œ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•˜๊ธฐ ์–ด๋ ต๋”๋ผ๊ตฌ์šฉ ํ”„๋ฆผ์œผ๋กœ ํ’€์–ด๋ณผ๊นŒ ํ–ˆ์ง€๋งŒ ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์‹œ์ž‘์ ์ด ์ •ํ•ด์ ธ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ์ข€ ๋” ๋ฐ”๋žŒ์งํ•˜๋‹ค๋Š” ํŒ๋‹จ. ๊ทธ๋ž˜์„œ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ ์ค‘๊ฐ„์— ํ•„์ˆ˜ ๋ฐฉ๋ฌธ๊นŒ์ง€ ์ฒดํฌํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ 1 ์—์„œ v1, v2๊นŒ์ง€ ๊ทธ๋ฆฌ๊ณ  v1, v2์—์„œ N ๊นŒ์ง€ ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ๋– ์˜ฌ๋ ธ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ v1์—์„œ v2 ํ˜น์€ v2์—์„œ v1์œผ๋กœ ๊ฐ€๋Š” ๊ฒƒ์„ ์ฒดํฌ์•ˆํ•ด์ค˜์„œ ๋˜ ์˜ˆ์™ธ์‚ฌํ•ญ ๋ฐœ๊ฒฌํ•˜๋Š”๋ฐ ํ•˜๋ฃจ๊ฐ€ ๊ฑธ๋ ธ๋„ค์š”

๋ฌธ์ œ ์ž˜ ๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค

ํ•„์ˆ˜๋กœ ์ง€๋‚˜๊ฐ€์•ผ ํ•˜๋Š” ๊ฒฝ๋กœ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ์‚ฌํ•ญ์„ ์ƒ๊ฐํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ €๋„ ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ v1์—์„œ v2 ๋˜๋Š” v2์—์„œ v1์œผ๋กœ ๊ฐ€๋Š”

๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋” ๊นŒ๋‹ค๋กœ์› ๋„ค์š”.

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ œ๋ฅผ ๋”ฑ ๋ดค์„ ๋•Œ 1 - v1/v2, v1/v2 - v1/v2, v1/v2 - n ์ด๋ ‡๊ฒŒ 3๊ฐœ์˜ ํŒŒํŠธ๋กœ ๋‚˜๋‰˜๊ณ , ์ด์— ๋Œ€ํ•œ ์ตœ์†Œ๋ฃจํŠธ๋ฅผ ๊ตฌํ•˜๋ฉด ๋˜๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๊ทธ ํ’€์ด๋Š” ๋งž์•˜๋„ค์š”! ๊ฐ ๋ถ€๋ถ„์— ๋Œ€ํ•ด MST๋ฅผ ๊ตฌํ•ด์„œ ์ง„ํ–‰ํ•ด๋„ ๊ดœ์ฐฎ์ง€ ์•Š์„๊นŒ ์ƒ๊ฐ์„ ํ•˜๋Š”๋ฐ ํ•œ๋ฒˆ ์‹œ๋„ํ•ด๋ด์•ผ๊ฒ ๋Š”๊ฑธ์š”?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants