Skip to content

Commit

Permalink
Merge pull request #171 from AlgoLeadMe/48-tgyuuAn
Browse files Browse the repository at this point in the history
48-tgyuuAn
  • Loading branch information
tgyuuAn authored May 3, 2024
2 parents a6ee561 + bfe4a47 commit 3c10fb8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@
| 45μ°¨μ‹œ | 2023.03.16 | 트라이 | <a href="https://www.acmicpc.net/problem/31413">트라이</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162
| 46μ°¨μ‹œ | 2023.03.20 | 트라이 | <a href="https://www.acmicpc.net/problem/27652">AB</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/165
| 47μ°¨μ‹œ | 2023.03.22 | μˆ˜ν•™, 뢄할정볡 | <a href="https://www.acmicpc.net/problem/15824">λ„ˆ λ΄„μ—λŠ” 캑사이신이 λ§›μžˆλ‹¨λ‹€</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/167
| 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,}
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만 μ‹œκ°„ λ³΅μž‘λ„ κ°€λŠ₯.

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

0 comments on commit 3c10fb8

Please sign in to comment.