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

18-alstjr7437 #66

Merged
merged 4 commits into from
Apr 8, 2024
Merged

18-alstjr7437 #66

merged 4 commits into from
Apr 8, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Mar 24, 2024

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

์ตœ๋‹จ ๊ฒฝ๋กœ

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

1์‹œ๊ฐ„(์‹œ๊ฐ„ ์ดˆ๊ณผ ์ฐพ๋Š”๋ฐ ๋งŽ์ด์”€..)

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

์šฐ์„  ์Œ์ˆ˜ ๊ฐ’๋„ ์—†๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ œ์ผ ๊ธฐ๋ณธ ๊ฐ™์€ ๋ฌธ์ œ๊ธธ๋ž˜ ์„ ์ •ํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!!

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์šฐ์„  ๋…ธ๋“œ์— ์ •์ ์„ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ๊ณ  "๊ฐ€์ค‘์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ" ๋นผ๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค!





image

์œ„์™€ ๊ฐ™์ด ๊ทธ๋ž˜ํ”„ ์˜ˆ์ œ๊ฐ€ ์žˆ๊ณ  1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๊ณ  ๊ฐ€์ •์„ ํ•˜๋ฉด



0 1 2 3 4 5 ์šฐ์„ ์ˆœ์œ„ ํ ์„ค๋ช…
INF 0 INF INF INF INF 1(0) ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์— 0
INF 0 2 3 INF INF 1(pop), 2(2 new!),3(3 new!) 1๋ฒˆ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ 2,3 ๋…ธ๋“œ ๊ฑฐ๋ฆฌ ์ถ”๊ฐ€ ๋ฐ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ๊ธฐ
INF 0 2 3 7 INF 2(pop), 3(3),4(7 new!) ์šฐ์„ ์ˆœ์œ„ ํ๋กœ ์ธํ•ด 2๊ฐ€ ๋จผ์ € ๋‚˜์˜ด, 3์€ 6๋ณด๋‹ค 3์ด ๋”์ž‘์œผ๋‹ˆ 3 ๊ทธ๋Œ€๋กœ
INF 0 2 3 7 INF 3(pop), 4(7) 3์ด ๋‚˜์˜ค์ง€๋งŒ 9๊ฐ€ 7๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐฑ์‹ X





  1. graph ๋งŒ๋“ค๊ธฐ
  2. ์šฐ์„ ์ˆœ์œ„ ํ์— ์‹œ์ž‘ ๋ถ€๋ถ„ ๋„ฃ๊ธฐ(๊ฐ€์ค‘์น˜๋Š” 0)
  3. ํž™์ด ๋นŒ๋•Œ๊นŒ์ง€ ์ง„ํ–‰
    1. ๋ณธ์ธ์˜ ๋ฌด๊ฒŒ์™€ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ ธ์˜ด
    2. ์ž์‹ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฒฐ๊ณผ์˜ ๊ฐ€์ค‘์น˜๋ณด๋‹ค ํฌ๋ฉด ๋ฆฌํ„ด
    3. ๋ณธ์ธ์˜ ๋…ธ๋“œ์™€ ๊ทธ๋ž˜ํ”„์— ์—ฐ๊ฒฐ๋œ ๋ถ€๋ถ„์„ ๋„ฃ๊ธฐ
    4. ๋ณธ์ธ์˜ ๋ฌด๊ฒŒ + ๊ฐ„์„ ์˜ ๋ฌด๊ฒŒ๋ฅผ ํ•ด์„œ ๊ธฐ์กด์˜ ๊ฐ’ ๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋„ฃ๊ธฐ
  4. ๊ฒฐ๊ณผ ์ถœ๋ ฅ




์œ„์™€ ๊ฐ™์ด ๋‹ค์ต์ŠคํŠธ๋ผ ๋– ์˜ฌ๋ฆฌ๊ณ  ํ’€์—ˆ๋Š”๋ฐ ์ฒ˜์Œ์— ํ‘ผ ์ฝ”๋“œ๋Š” ๊ณ„์†ํ•ด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค!
์ด๊นŒ์ง€ ์ฝ๊ณ  ํ•œ๋ฒˆ ์–ด๋””์„œ ํ‹€๋ ธ๋Š”์ง€ ์ฐพ์•„๋ณด๋Š” ๋ถ€๋ถ„๋„ ์žฌ๋ฐŒ์–ด์š”!


from heapq import *
import sys

input = sys.stdin.readline

v, e = map(int, input().split())
k = int(input())

# ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ
graph = [[] for _ in range(e + 1)]
for i in range(e):
    a,b,c = map(int, input().split())
    graph[a].append((b,c))

# ๊ฒฐ๊ณผ ์ €์žฅํ•  ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ
heap = []
result = [int(1e9)] * (v + 1)

# k ๋„ฃ๊ธฐ
result[k] = 0
heappush(heap, (k, 0))
# ๋‹ค์ต์ŠคํŠธ๋ผ ๋Œ๋ฆฌ๊ธฐ
while heap:
    now, weight = heappop(heap)

    if result[now] < weight:
        continue
    for i in graph[now]:
        next_weight = weight + i[1]
        if next_weight < result[i[0]]:
            result[i[0]] = next_weight
            heappush(heap, (i[0], next_weight))

# ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ
for i in range(1,v+1):
    if result[i] == int(1e9):
        print("INF")
    else :
        print(result[i])






์ตœ์ข… ์ฝ”๋“œ

์œ„ ๋ถ€๋ถ„์—์„œ ์ด์ƒํ–ˆ๋˜ ๋ถ€๋ถ„์€ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ์„๋•Œ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๋ฅผ ์šฐ์„ ์œผ๋กœ ์ค˜์„œ
๊ฐ€์ค‘์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋จผ์ € ๋น ์ง€๋Š”๊ฒŒ ์•„๋‹Œ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋จผ์ € ๋นผ์ค˜์„œ
๋‹ต์€ ๋งž๊ฒŒ ๋์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค.




๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๋ฉด
image

์œ„์™€ ๊ฐ™์€ ๊ทธ๋ž˜ํ”„๊ฐ€ ์žˆ์œผ๋ฉด
์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ฐ€์ค‘์น˜๋กœ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด
1 -> 3 -> 2 -> 4 -> 2๋กœ ๋˜์ง€๋งŒ

์œ„์— ๋ฐฉ์‹๋Œ€๋กœ ํ‹€๋ฆฐ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋Œ๋ ค๋ณด๋ฉด
1 -> 2 -> 4 -> 3 -> 2 -> 4๋กœ ์—„์ฒญ๋‚˜๊ฒŒ ๊ฐ„๋‹จํ•œ ๊ทธ๋ž˜ํ”„์—ฌ๋„ ์—ฐ์‚ฐ์ด ํ•œ๋ฒˆ ๋Š˜์–ด๋‚œ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.




์‹ค์ œ๋กœ ๋Œ๋ ค๋ณด๋‹ˆ

  • ๊ฐ€์ค‘์น˜๋กœ ์šฐ์„ ์ˆœ์œ„ ํ
๋ฌด๊ฒŒ ๋…ธ๋“œ ๊ฒฐ๊ณผ
0 1 [1000000000, 0, 1000000000, 1000000000, 1000000000]
1 3 [1000000000, 0, 5, 1, 1000000000]
3 2 [1000000000, 0, 3, 1, 3]
3 4 [1000000000, 0, 3, 1, 3]
5 2 [1000000000, 0, 3, 1, 3]



  • ๋…ธ๋“œ ๋ฒˆํ˜ธ๋กœ ์šฐ์„ ์ˆœ์œ„ ํ
๋ฌด๊ฒŒ ๋…ธ๋“œ ๊ฒฐ๊ณผ
0 1 [1000000000, 0, 1000000000, 1000000000, 1000000000]
5 2 [1000000000, 0, 5, 1, 1000000000]
1 3 [1000000000, 0, 5, 1, 7]
3 2 [1000000000, 0, 3, 1, 3]
3 4 [1000000000, 0, 3, 1, 3]
7 4 [1000000000, 0, 3, 1, 3]

์ตœ์ข… ์ฝ”๋“œ

from heapq import *
import sys

input = sys.stdin.readline

v, e = map(int, input().split())
k = int(input())

# ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ
graph = [[] for _ in range(v + 1)]
for _ in range(e):
    a, b, c = map(int, input().split())
    graph[a].append((c, b))  

# ๊ฒฐ๊ณผ ์ €์žฅํ•  ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ
heap = []
result = [int(1e9)] * (v + 1)

# k ๋„ฃ๊ธฐ
result[k] = 0
heappush(heap, (0, k))  

# ๋‹ค์ต์ŠคํŠธ๋ผ ๋Œ๋ฆฌ๊ธฐ
while heap:
    weight, now = heappop(heap)  
    # print(weight,now, heap, result)
    if result[now] < weight:
        continue
    for next_weight, next_node in graph[now]:
        total_weight = weight + next_weight
        if total_weight < result[next_node]:
            result[next_node] = total_weight
            heappush(heap, (total_weight, next_node))

# ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ
for i in range(1, v + 1):
    if result[i] == int(1e9):
        print("INF")
    else:
        print(result[i])

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

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋…์„ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋Œ€ํ•ด์„œ๋„ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๋Š” ์ข‹์€ ๊ณ„๊ธฐ๊ฐ€ ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

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

์•„์ง ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๋‹ค์ต์ŠคํŠธ๋ผ, ์˜ค๋Š˜ PR ์— ํ•œ ๋ฒˆ ๋„์ „ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿฅน

import sys
from heapq import *

input=sys.stdin.readline

vertex, edge = map(int, input().split())
start_vertex = int(input())
graph = {v : [] for v in range(1,vertex+1)}
for _ in range(edge) :
    start, end, weight = map(int, input().split())
    graph[start].append((end, weight))
print(graph)
heap = []
result = [int(1e9)] * (vertex+1)
result[start_vertex] = 0
heappush(heap, (start_vertex,0))

while heap :
    current_node, current_weight = heappop(heap)
    print(current_node, current_weight, end=" : ")
    if result[current_node] < current_weight :
        continue
    for node, weight in graph[current_node]:
        print(node, weight)
        distance = current_weight + weight
        if distance < result[node]:
            result[node] = distance
            heappush(heap, (node, distance))

for i in range(1,vertex+1):
    if result[i] == int(1e9):
        print('INF')
    else :
        print(result[i])

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

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

์˜ค,, ๊ทธ๋Ÿฌ๊ณ ๋ณด๋‹ˆ ๋…ธ๋“œ ๋ฒˆํ˜ธ๊ฐ€ ์•ž์— ์žˆ์œผ๋ฉด ํž™ํ์—์„œ ๋บ„ ๋•Œ ๊ฐ€์ค‘์น˜ ๊ธฐ์ค€์ด ์•„๋‹ˆ๋ผ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋จผ์ € ๋นผ๊ฒ ๋„ค์š”. ์ˆœ์„œ๋„ ๋ฌธ์ œ ์กฐ๊ฑด์„ ์ž˜ ์ฝ๊ณ  ํ•ด๋ณด๋ฉด ๋‹ค ๋น ๋ฅด๊ฒŒ ๋๋‚ด๋ณผ ์ˆ˜ ์žˆ๊ฒ ์–ด์š”. ์ข‹์€๊ฑฐ ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค ! ์ €๋„ ์ด ๋ฌธ์ œ ํ’€์—ˆ์—ˆ๋Š”๋ฐ ์šด ์ข‹?๊ฒŒ ๊ฐ€์ค‘์น˜๊ฐ€ ์•ž์— ์žˆ๊ตฐ์š”

@SeongHoonC SeongHoonC removed their request for review April 3, 2024 14:59
@alstjr7437 alstjr7437 merged commit 14500e3 into main Apr 8, 2024
3 checks passed
@alstjr7437 alstjr7437 deleted the 18-alstjr7437 branch April 8, 2024 04:57
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.

4 participants