Created
September 14, 2021 02:49
-
-
Save kjsman/2a9650bc9ee0a7eb08c456e171b0bae0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n, m = map(int, input().split()) | |
g = {i: [] for i in range(n)} | |
for _ in range(m): | |
a, b, c = map(int, input().split()) | |
g[a - 1].append([b - 1, c]) | |
ds = [0] + [float('inf')] * (n - 1) | |
def cycle(): | |
ran = False | |
for a in g: | |
for b, c in g[a]: | |
if ds[b] > ds[a] + c: | |
ran = True | |
ds[b] = ds[a] + c | |
return ran | |
for _ in range(n): | |
cycle() | |
if cycle(): | |
print(-1) | |
exit() | |
for d in ds[1:]: | |
print(d if d < float('inf') else -1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment