-
코드
import sys inf = sys.maxsize n, m= map(int, input().split()) destination = [inf]*(n+1) tree = list([] for _ in range(n+1)) is_possible = True def bellman(start): global is_possible destination[start] = 0 for repeat in range(n): for i in range(1,n+1): for node, weight in tree[i]: new_weight = destination[i] + weight if new_weight < destination[node] and destination[i] != inf: destination[node] = new_weight if repeat == n-1: is_possible= False for _ in range(m): u, v, w = map(int, input().split()) tree[u].append([v, w]) bellman(1) if not is_possible: print(-1) else: for i in destination[2:]: print(i if i != inf else "-1")
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 3273번 두 수의 합 (파이썬) (0) 2022.01.05 [백준] 11404번 플로이드 (파이썬) (0) 2022.01.05 [백준] 1504번 특정한 최단 경로 (파이썬) (0) 2022.01.05 [백준] 1753번 최단경로 (파이썬) (0) 2022.01.05 [백준] 7562번 나이트의 이동 (파이썬) (0) 2022.01.05