Skip to content

Commit

Permalink
Merge pull request #244 from AlgoLeadMe/28-H0ngJu
Browse files Browse the repository at this point in the history
28-H0ngJu
  • Loading branch information
H0ngJu authored Sep 20, 2024
2 parents a654144 + 7a6bd20 commit 0138dc5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion H0ngJu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
| 25์ฐจ์‹œ | 2024.08.17 | DFS | [์นœ๊ตฌ๋น„](https://www.acmicpc.net/problem/16562) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/234 |
| 26์ฐจ์‹œ | 2024.08.24 | ๊ทธ๋ฆฌ๋”” | [์‹ ์ž…์‚ฌ์›](https://www.acmicpc.net/problem/1946) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/237 |
| 27์ฐจ์‹œ | 2024.08.27 | DFS | [ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„](https://www.acmicpc.net/problem/1967) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/240 |

| 28์ฐจ์‹œ | 2024.09.04 | ๋ฒจ๋งŒํฌ๋“œ | [ํƒ€์ž„๋จธ์‹ ](https://www.acmicpc.net/problem/11657) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/244 |
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys

INF = 1e8

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

N, M = map(int, input().split())
bus = [[] for _ in range(N+1)]
distance = [INF] * (N+1)

for i in range(M):
A, B, C = map(int, input().split())
bus[A].append([B, C])

def BellmanFord(start):
distance[start] = 0

for i in range(N):
for j in range(1, N+1):
for n, w in bus[j]:
if distance[j] != INF and distance[n] > distance[j] + w:
distance[n] = distance[j] + w
if i == N-1:
return True
return False

check_negative = BellmanFord(1)

if check_negative:
print("-1")
else:
for i in range(2, N+1):
if distance[i] == INF:
print("-1")
else:
print(distance[i])

0 comments on commit 0138dc5

Please sign in to comment.