import sys
def solution():
check = [float('inf')] * (N + 1)
check[1] = 0
for n in range(1, N+1):
for i in range(1, N+1):
if check[i] == float('inf'):
continue
for k, l in dist[i].items():
l += check[i]
if check[k] > l:
if n == N:
return [-1]
check[k] = l
return check[2:]
N, M = map(int, sys.stdin.readline().split())
dist = {i: {} for i in range(1, N+1)}
for _ in range(M):
a, b, c = map(int, sys.stdin.readline().split())
if b in dist[a]:
dist[a][b] = min(dist[a][b], c)
else:
dist[a][b] = c
for ans in solution():
if ans == float('inf'):
print(-1)
else:
print(ans)
'파이썬 코테 준비' 카테고리의 다른 글
[백준 1717] 집합의 표현 - Python(파이썬) (0) | 2021.03.11 |
---|---|
[백준 6549] 히스토그램에서 가장 큰 직사각형 - Python(파이썬) (0) | 2021.03.11 |
[백준 9370] 미확인 도착지 - Python(파이썬) (0) | 2021.03.11 |
[백준 1504] 특정한 최단 경로 - Python(파이썬) (0) | 2021.03.11 |
[백준 1753] 최단경로 - Python(파이썬) (0) | 2021.03.11 |
댓글