-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ11657.py
More file actions
40 lines (30 loc) · 770 Bytes
/
BJ11657.py
File metadata and controls
40 lines (30 loc) · 770 Bytes
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
import sys
input = sys.stdin.readline
INF = int(1e9)
n, m = map(int, input().split())
edges = list()
time = [INF] * n
def bf(start):
time[start] = 0
for i in range(1, n + 1):
for j in range(m):
now, next, t = edges[j][0], edges[j][1], edges[j][2]
if time[now] != INF and time[next] > time[now] + t:
time[next] = time[now] + t
if i == n:
return True
return False
for i in range(m):
a, b, c = map(int, input().split())
edges.append((a, b, c))
negative_cycle = bf(1)
print(time)
print(edges)
if negative_cycle == True:
print(-1)
else:
for i in range(2, n + 1):
if time[i] == INF:
print(-1)
else:
print(time[i])