느낀점
정답 코드 및 해설
N, M = map(int, input().split())
possible = True
nodes = [float('inf') for _ in range(N)]
nodes[0] = 0
edges = []
for _ in range(M):
a, b, c = map(int, input().split())
edges.append([a-1, b-1, c])
for _ in range(M-1):
for i in range(M):
a, b, c = edges[i]
if nodes[a] != float('inf'):
temp = nodes[a] + c
if temp < nodes[b]:
nodes[b] = temp
for i in range(M):
a, b, c = edges[i]
if nodes[a] != float('inf'):
temp = nodes[a] + c
if temp < nodes[b]:
possible = False
if possible:
for node in nodes[1:]:
if node != float('inf'):
print(node)
else:
print(-1)
else:
print(-1)