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

48-tgyuuAn #171

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@
| 43μ°¨μ‹œ | 2024.03.10 | 이뢄 탐색 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/64062">징검닀리 κ±΄λ„ˆκΈ°</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/157
| 44μ°¨μ‹œ | 2023.03.13 | 트라이 | <a href="https://www.acmicpc.net/problem/14725">개미꡴</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159
| 45μ°¨μ‹œ | 2023.03.16 | 트라이 | <a href="https://www.acmicpc.net/problem/31413">트라이</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162
| 48μ°¨μ‹œ | 2023.03.25 | 벨만 ν¬λ“œ | <a href="https://www.acmicpc.net/problem/1738">골λͺ©κΈΈ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/171
---
78 changes: 78 additions & 0 deletions tgyuuAn/벨만 ν¬λ“œ/골λͺ©κΈΈ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import sys
from collections import deque

def input(): return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
edge = [[] for _ in range(N+1)]

# κ°„μ„  정보 λ°›μŒ
for _ in range(M):
start, destination, cost = map(int,input().split())
edge[start].append([destination, cost])

# 초기 μ„ΈνŒ…
board = [-int(1e9) for _ in range(N+1)]
board[1] = 0

# 졜적의 경둜λ₯Ό μ°ΎκΈ° μœ„ν•΄ 역좔적 ν•˜κΈ° μœ„ν•΄μ„œ 이전 λ…Έλ“œλ₯Ό 기둝
prev_node = [-1 for _ in range(N+1)]
prev_node[1] = 0

for _ in range(N-1):
for start in range(1,N+1):
for destination, cost in edge[start]:
if board[destination] < board[start] + cost:
board[destination] = board[start] + cost
prev_node[destination] = start

has_cycle = False
is_connect_target = False
for start in range(1,N+1):
for destination, cost in edge[start]:
# 사이클 λ°œμƒ
if board[destination] < board[start] + cost:
has_cycle = True

# 사이클이 λ°œμƒν•΄λ„ κ²½λ‘œλž‘ 관련이 없을 μˆ˜λ„ μžˆμœΌλ―€λ‘œ,
# 사이클이 λ°œμƒν•œ 지점이 λͺ©ν‘œ 지점과 관련이 μžˆλŠ”μ§€ 체크크
deq = deque([start])
visited = {start,}
Copy link
Collaborator

@H0ngJu H0ngJu Mar 29, 2024

Choose a reason for hiding this comment

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

μ‚¬μ†Œν•œκ±°κΈ΄ ν•œλ°, start 뒀에 ,<< 이건 별 의미 μ—†λŠ” κ±΄κ°€μš”? πŸ€”

Copy link
Member Author

Choose a reason for hiding this comment

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

집합을 μ„ μ–Έν•  λ•Œμ—λŠ” 항이 ν•˜λ‚˜ 밖에 없을 λ•Œ 콀마λ₯Ό λ„£μ–΄μ€˜μ•Ό ν•΄μš”!

μœ„ μ½”λ“œλŠ”

visited = set()
visited.add(start)

λž‘ λ™μΌν•˜κ²Œ μž‘λ™ν•©λ‹ˆλ‹€!

while deq:
now = deq.popleft()

for d, c in edge[now]:
if d in visited: continue

deq.append(d)
visited.add(d)

# 사이클이 있고 λͺ©ν‘œμ§€μ  ν˜Ήμ€ μ‹œμž‘μ§€μ κ³Ό λΆ™μ–΄μžˆμœΌλ©΄ -1
if d == 1 or d == N:
is_connect_target = True
break

if is_connect_target: break
break

# 사이클이 μžˆλŠ”λ° ν•΄λ‹Ή 사이클이 λͺ©ν‘œμ™€ μ—°κ²°λ˜μ–΄ μžˆμ„ 경우
if has_cycle and is_connect_target: print(-1)
else:
answer = []
now = N
while now != 1:
answer.append(now)
now = prev_node[now]

answer.append(now)

if now != 1: print(-1)
else: print(*answer[::-1])

# 총 κ°„μ„  = 2만개,
# 총 λ…Έλ“œ = 100개
# 벨만 ν¬λ“œ = ( κ°„μ„  X λ…Έλ“œ -1 ) -> 198만 μ‹œκ°„ λ³΅μž‘λ„ κ°€λŠ₯.

# 졜적의 경둜
# 사이클이 λ°œμƒν•΄λ„ 갈 수 μžˆμ„ 수 있음.
# 사이클이 없더라도 도달할 수 없을 수 있음.