Skip to content

Commit d593d25

Browse files
committed
2024-03-25
1 parent 4b04141 commit d593d25

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

β€ŽtgyuuAn/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@
4747
| 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
4848
| 44μ°¨μ‹œ | 2023.03.13 | 트라이 | <a href="https://www.acmicpc.net/problem/14725">개미꡴</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159
4949
| 45μ°¨μ‹œ | 2023.03.16 | 트라이 | <a href="https://www.acmicpc.net/problem/31413">트라이</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162
50+
| 48μ°¨μ‹œ | 2023.03.25 | 벨만 ν¬λ“œ | <a href="https://www.acmicpc.net/problem/1738">골λͺ©κΈΈ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/171
5051
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import sys
2+
from collections import deque
3+
4+
def input(): return sys.stdin.readline().rstrip()
5+
6+
N, M = map(int, input().split())
7+
edge = [[] for _ in range(N+1)]
8+
9+
# κ°„μ„  정보 λ°›μŒ
10+
for _ in range(M):
11+
start, destination, cost = map(int,input().split())
12+
edge[start].append([destination, cost])
13+
14+
# 초기 μ„ΈνŒ…
15+
board = [-int(1e9) for _ in range(N+1)]
16+
board[1] = 0
17+
18+
# 졜적의 경둜λ₯Ό μ°ΎκΈ° μœ„ν•΄ 역좔적 ν•˜κΈ° μœ„ν•΄μ„œ 이전 λ…Έλ“œλ₯Ό 기둝
19+
prev_node = [-1 for _ in range(N+1)]
20+
prev_node[1] = 0
21+
22+
for _ in range(N-1):
23+
for start in range(1,N+1):
24+
for destination, cost in edge[start]:
25+
if board[destination] < board[start] + cost:
26+
board[destination] = board[start] + cost
27+
prev_node[destination] = start
28+
29+
has_cycle = False
30+
is_connect_target = False
31+
for start in range(1,N+1):
32+
for destination, cost in edge[start]:
33+
# 사이클 λ°œμƒ
34+
if board[destination] < board[start] + cost:
35+
has_cycle = True
36+
37+
# 사이클이 λ°œμƒν•΄λ„ κ²½λ‘œλž‘ 관련이 없을 μˆ˜λ„ μžˆμœΌλ―€λ‘œ,
38+
# 사이클이 λ°œμƒν•œ 지점이 λͺ©ν‘œ 지점과 관련이 μžˆλŠ”μ§€ 체크크
39+
deq = deque([start])
40+
visited = {start,}
41+
while deq:
42+
now = deq.popleft()
43+
44+
for d, c in edge[now]:
45+
if d in visited: continue
46+
47+
deq.append(d)
48+
visited.add(d)
49+
50+
# 사이클이 있고 λͺ©ν‘œμ§€μ  ν˜Ήμ€ μ‹œμž‘μ§€μ κ³Ό λΆ™μ–΄μžˆμœΌλ©΄ -1
51+
if d == 1 or d == N:
52+
is_connect_target = True
53+
break
54+
55+
if is_connect_target: break
56+
break
57+
58+
# 사이클이 μžˆλŠ”λ° ν•΄λ‹Ή 사이클이 λͺ©ν‘œμ™€ μ—°κ²°λ˜μ–΄ μžˆμ„ 경우
59+
if has_cycle and is_connect_target: print(-1)
60+
else:
61+
answer = []
62+
now = N
63+
while now != 1:
64+
answer.append(now)
65+
now = prev_node[now]
66+
67+
answer.append(now)
68+
69+
if now != 1: print(-1)
70+
else: print(*answer[::-1])
71+
72+
# 총 κ°„μ„  = 2만개,
73+
# 총 λ…Έλ“œ = 100개
74+
# 벨만 ν¬λ“œ = ( κ°„μ„  X λ…Έλ“œ -1 ) -> 198만 μ‹œκ°„ λ³΅μž‘λ„ κ°€λŠ₯.
75+
76+
# 졜적의 경둜
77+
# 사이클이 λ°œμƒν•΄λ„ 갈 수 μžˆμ„ 수 있음.
78+
# 사이클이 없더라도 도달할 수 없을 수 있음.

0 commit comments

Comments
Β (0)