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

28-H0ngJu #244

Merged
merged 2 commits into from
Sep 20, 2024
Merged

28-H0ngJu #244

merged 2 commits into from
Sep 20, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Sep 4, 2024

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

ํƒ€์ž„๋จธ์‹ 

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

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

image

๋ฌธ์ œ ์š”์•ฝ
1๋ฒˆ ๋„์‹œ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๊ฐ ๋„์‹œ๋กœ ๊ฐ€๋Š” ๊ฐ€์žฅ ๋น ๋ฅธ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋ผ. ๋ฌดํ•œํžˆ ๋˜๋Œ์•„ ๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š” -1, ๊ฒฝ๋กœ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋„ -1์„ ์ถœ๋ ฅํ•˜๋ผ



์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ œ๋ชฉ ๋ณด๊ณ  ๊ทธ๋ƒฅ ๊ณ ๋ฅธ ๋ฌธ์ œ๋ผ

์ฒ˜์Œ์— ๋ฌธ์ œ๋ณด๊ณ  ์˜ค? ์ตœ๋‹จ๊ฑฐ๋ฆฌ? ์ด๊ฑด ๋‹ค์ต์ŠคํŠธ๋ผ ์“ฐ๋ฉด ๋˜๊ฒ ๋‹ค ํ–ˆ๋Š”๋ฐ


์Œ์˜ ๊ฐ€์ค‘์น˜ ๋ฅผ ๋ณด๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋…ธํŠธ์— ์žˆ๋˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋– ์˜ฌ๋ž์Šต๋‹ˆ๋‹ค ...


๋‚˜์˜๋‹˜์ด ์˜›๋‚ ์— ์˜ฌ๋ฆฌ์…จ๋˜ ๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ƒ๊ฐ๋‚˜๋”๋ผ๊ณ ์š”.


๋ฌธ์ œ์—์„œ ๋งํ•˜๋Š” '๋ฌดํ•œํžˆ ๊ณผ๊ฑฐ๋กœ ๋˜๋Œ์•„ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ'๊ฐ€
๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ํ’€๋ฉด ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๋ฌดํ•œํžˆ ์—…๋ฐ์ดํŠธ ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์—,


์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๋ฌธ์ œ์—, ์Œ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š”
์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๋” ํฌ๋”๋ผ๋„ ๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ธ€์„ ๋ดค๋˜ ๊ธฐ์–ต์ด ๋– ์˜ฌ๋ž์Šต๋‹ˆ๋‹ค.


image


๊ทธ๋Ÿฌ๊ณ  ๋‹ค์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋…ธํŠธ๋ฅผ ๋ณด๋‹ˆ๊นŒ ๋ฌธ์ œ์— ํƒ€์ž„๋จธ์‹  ๋ฌธ์ œ๊ฐ€ ์žˆ๋”๋ผ๊ณ ์šฉ ํ—ˆํ—ˆ


๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด ๋‹ค์ต์ŠคํŠธ๋ผ๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์„ ํƒํ•ด์„œ ๊ทธ๋ฆฌ๋””ํ•˜๊ฒŒ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค๋ฉด,

๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋Š” ๋ชจ๋“  ๊ฐ„์„ ์„ ๋‹ค ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ๋ณด๋‹ค ํด ์ˆ˜ ๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ํ™•์ธํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋…ธ๋“œ๊ฐ€ n๊ฐœ๊ฐ€ ์žˆ๋‹ค๋ฉด n-1๋ฒˆ๋™์•ˆ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐ๋œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ์—์„œ ํ•œ๋ฒˆ ๋” ํƒ์ƒ‰ํ•ด์„œ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋˜๋ฉด, ์Œ์ˆ˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์žˆ๊ณ , ์ด๋กœ ์ธํ•ด ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.



ํƒ€์ž„ ๋จธ์‹  ๋ฌธ์ œ์—์„œ ๋“ฑ์žฅํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1๋ฒˆ์ด ์‹œ์ž‘์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ distance๋Š” ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

image

๊ฐ„์„  1๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—…๋ฐ์ดํŠธ ๋ฉ๋‹ˆ๋‹ค.

image

๊ฐ„์„  2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š”

3๋ฒˆ ๋„์‹œ์˜ ๊ฒฝ์šฐ, 1->2, 2->3 ๋กœ 0์œผ๋กœ ์—…๋ฐ์ดํŠธ ๋ฉ๋‹ˆ๋‹ค.

image

๊ฐ„์„  3๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š”,

image

์ด๋ ‡๊ฒŒ distance[1]์ด 1->2, 2->3, 3->1๋กœ ๊ฐ€๋Š” ๊ณผ์ •์— ๋”ฐ๋ผ ์—…๋ฐ์ดํŠธ ๋˜๋ฏ€๋กœ ๋ฌดํ•œ ์‚ฌ์ดํด์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ํ•ด๋‹น ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ -1์„ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค!

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

@tgyuuAn
Copy link
Member

tgyuuAn commented Sep 4, 2024

@gjsk132 ์™€์šฐ ๋‚˜๋ด‰

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

import sys

city_count, bus_count = map(int,sys.stdin.readline().split())
city_table = [int(1e9) for _ in range(city_count+1)]
city_table[1] = 0
bus_information = []

for _ in range(bus_count):
    bus_information.append(list(map(int,sys.stdin.readline().split())))
    
cycle_flag = False
for i in range(city_count):
    for start, destination, cost in bus_information:
        if city_table[start] != int(1e9) and city_table[destination] > city_table[start] + cost:
            city_table[destination] = city_table[start] + cost

            if i == city_count-1:
                cycle_flag = True

if cycle_flag: print(-1)
else:
    for cost in city_table[2:]:
        if cost == int(1e9): print(-1)
        else: print(cost)   

๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์—์„œ ์Œ์˜๊ฐ„์„ ? -> ๋ฒจ๋งŒํฌ๋“œ

๋ฒจ๋งŒํฌ๋“œ ? -> ๋…ธ๋“œ๊ฐœ์ˆ˜ -1 ๋ฒˆ ๋งŒํผ ๋ชจ๋“  ๊ฒฝ๋กœ ํƒ์ƒ‰

๋…ธ๋“œ ๊ฐœ์ˆ˜ -1 ํ•œ๋ฒˆ ๋” ๋Œ๋ ธ์„ ๋•Œ ๊ฐ’์ด ๋ฐ”๋€๋‹ค? -> ์Œ์˜ ์‚ฌ์ดํด์ด ์ƒ๊ธด ๊ฒƒ

์ด๊ฒƒ๋งŒ ๊ธฐ์–ตํ•ด!!!!!!!!!!!!!!!

@gjsk132
Copy link
Member

gjsk132 commented Sep 4, 2024

์šฐ์™•!
์—ด์‹ฌํžˆ ์“ด ๊ธ€์ด ์ด๋ ‡๊ฒŒ ๋“ฑ์žฅํ•˜๋‹ˆ๊นŒ ์–ด๊นจ๊ฐ€ ์œผ์“ฑ์œผ์“ฑํ•˜๋„ค์š” XD

@H0ngJu H0ngJu merged commit 0138dc5 into main Sep 20, 2024
@H0ngJu H0ngJu deleted the 28-H0ngJu branch September 20, 2024 01:12
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