-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ_11657.py
65 lines (59 loc) · 1.59 KB
/
BOJ_11657.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# import sys
# INF = sys.maxsize
# input = sys.stdin.readline
#
# def relax():
# for start in range(n+1):
# for end, weight in graph[start]:
# next_weight = dist[start] + weight
# if dist[end] > next_weight:
# dist[end] = next_weight
# return dist
#
# def bellman():
# for start in range(n+1):
# for end, weight in graph[start]:
# next_weight = dist[start] + weight
# if dist[end] > next_weight:
# return False
# return True
#
# n, m = map(int, input().split())
# graph = [[] for _ in range(n+1)]
# dist = [INF for _ in range(n+1)]
# dist[1] = 0
#
# for _ in range(m):
# s, e, w = map(int, input().split())
# graph[s].append([e, w])
#
# short = relax()
#
# if bellman():
# for i in range(2, n+1):
# print(short[i] if short[i] != INF else -1)
# else:
# print(-1)
import sys
INF = sys.maxsize
input = sys.stdin.readline
def bellman():
for i in range(1, n+1):
for start in range(1, n+1):
for end, weight in graph[start]:
next_weight = weight + dist[start]
if dist[end] > next_weight:
if i == n and next_weight < 60000001:
print(-1)
sys.exit(0)
dist[end] = next_weight
n, m = map(int, input().rstrip().split())
graph = [[] for _ in range(n+1)]
dist = [INF for _ in range(n+1)]
dist[1] = 0
for _ in range(m):
s, e, w = map(int, input().rstrip().split())
graph[s].append([e, w])
bellman()
for i in dist[2:]:
print(i if i < 60000001 else -1)