-
Notifications
You must be signed in to change notification settings - Fork 0
/
d10.py
353 lines (296 loc) · 9.04 KB
/
d10.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# Tìm đường đi tốt nhất theo 1 tiêu chí nào đó, thì bellman có thể phát hiện chu trình mà làm cho độ "tốt" luôn tăng
# # Wormholes
# INF = int(1e9)
# def bell_man_ford(s, m, n, edge_list):
# dist = [INF for index in range(n)]
# dist[s] = 0
# for i in range(1, n):
# for j in range(m):
# cx, cy, ct = edge_list[j]
# if dist[cx] != INF and dist[cx] + ct < dist[cy]:
# dist[cy] = dist[cx] + ct
# for i in range(m):
# cx, cy, ct = edge_list[i]
# if dist[cx] != INF and dist[cx] + ct < dist[cy]:
# return False
# return True
# # Input
# c = int(input())
# for _ in range(c):
# # n is the number of star systems a.k.a vertices
# # m is the number of wormholes a.k.a edges
# n, m = list(map(int, input().split()))
# edge_list = []
# for i in range(m):
# x, y, t = list(map(int, input().split()))
# edge_list.append((x, y, t))
# # Run
# res = bell_man_ford(0, m, n, edge_list)
# if not res:
# print('possible')
# else:
# print('not possible')
# # Extended traffic
# INF = int(1e9)
# def bell_man_ford(s, m, n, edge_list):
# dist = [INF for index in range(n + 1)]
# dist[s] = 0
# for i in range(n - 1): # n-1 thôi là đủ
# for j in range(m):
# cx, cy, ct = edge_list[j]
# if dist[cx] != INF and dist[cx] + ct < dist[cy]:
# dist[cy] = dist[cx] + ct
# # đánh dấu nhưng đỉnh bị ảnh hưởng bởi chu trình âm
# for i in range(n - 1):
# for j in range(m):
# cx, cy, ct = edge_list[j]
# # nếu đỉnh cy còn update được dist thì đánh dấu lại
# if dist[cx] != INF and dist[cx] + ct < dist[cy]:
# dist[cy] = -INF # dist gán bằng âm vô cùng --> bị ảnh hưởng bởi chu trình âm
# return dist
# t = int(input())
# for case in range(t):
# input()
# n = int(input()) # vertices
# # n intergers denoting the busyness of the junctions
# vertices = [0]
# busyness_of_junctions = list(map(int, input().split()))
# vertices = [*vertices, *busyness_of_junctions]
# edge_list = []
# m = int(input()) # edges
# for i in range(m):
# u, v = list(map(int, input().split()))
# edge_list.append((u, v, (vertices[v] - vertices[u]) ** 3))
# q = int(input()) # number of queries
# queries = []
# for i in range(q):
# query = int(input())
# queries.append(query)
# dist = bell_man_ford(1, m, n, edge_list)
# print('Case {}:'.format(case + 1))
# for query in queries:
# if 3 <= dist[query] < INF:
# print(dist[query])
# else:
# print('?')
# # XYZZY
# import queue
# INF = int(1e9)
# def hasPathBFS(s, n, edge_list):
# visited = [False] * (n + 1)
# q = queue.Queue()
# q.put(s)
# visited[s] = True
# while not q.empty():
# u = q.get()
# for edge in edge_list:
# s, t = edge
# if s == u:
# if not visited[t]:
# visited[t] = True
# q.put(t)
# if t == n:
# return True
# return False
# def bell_man_ford(s, n, edge_list, rooms_point):
# dist = [-INF for index in range(n + 1)]
# dist[s] = 100
# for i in range(n - 1):
# for edge in edge_list:
# cx, cy = edge
# if dist[cx] > 0:
# dist[cy] = max(dist[cy], dist[cx] + rooms_point[cy])
# for edge in edge_list:
# cx, cy = edge
# if dist[cx] > 0 and dist[cx] + rooms_point[cy] > dist[cy] and hasPathBFS(cx, n, edge_list):
# return True
# return dist[n] > 0
# while True:
# n = int(input())
# if n == -1:
# break
# edge_list = []
# adj_list = [[] for index in range(n + 1)]
# rooms_point = [0 for index in range(n + 1)]
# for i in range(n):
# w, s, *a = list(map(int, input().split()))
# current_room = i + 1
# rooms_point[current_room] = w
# adj_list[current_room] = a
# for i in range(len(adj_list)):
# if i != 0:
# for j in adj_list[i]:
# edge_list.append((i, j))
# can_win = bell_man_ford(1, n, edge_list, rooms_point)
# if can_win:
# print('winnable')
# else:
# print('hopeless')
# 106 Miles To Chicago
'''
As they are on a mission from God,
you should help them find the safest way to Chicago.
In this problem, the safest way is considered to be the route which maximises the probability that they are not caught.
'''
'''
Nếu một công việc nào đó phải hoàn thành qua nn giai đoạn liên tiếp, trong đó:
Phương án thứ 1 có m_1
Phương án thứ 2 có m_2
…
Phương án thứ n có m_n
Khi đó sẽ có m_1 * m_2 * ... * m_n cách để hoàn thành công việc được cho.
'''
# def bell_man_ford(s, n, graph):
# dist = [-1.0 for index in range(n + 1)]
# dist[s] = 1.0
# for _ in range(n - 1): # loop n-1 times
# for edge in graph:
# u, v, r = edge
# dist[u] = max(dist[u], dist[v] * r)
# dist[v] = max(dist[v], dist[u] * r)
# return dist[n]
# while True:
# line = list(map(int, input().split()))
# if len(line) == 1:
# break
# # n is the number of intersections
# # m is the number of streets to be considered.
# n, m = line
# graph = []
# for _ in range(m):
# a, b, p = list(map(int, input().split()))
# graph.append((a, b, p / 100))
# result = bell_man_ford(1, n, graph)
# print('{:.6f} percent'.format(result * 100))
# Single source shortest path, negative weights
# INF = int(1e9)
# def bell_man_ford(s, m, n, edge_list):
# dist = [INF for index in range(n + 1)]
# dist[s] = 0
# for i in range(n - 1): # n-1 thôi là đủ
# for j in range(m):
# cx, cy, ct = edge_list[j]
# if dist[cx] != INF and dist[cx] + ct < dist[cy]:
# dist[cy] = dist[cx] + ct
# # đánh dấu nhưng đỉnh bị ảnh hưởng bởi chu trình âm
# for i in range(n - 1):
# for j in range(m):
# cx, cy, ct = edge_list[j]
# # nếu đỉnh cy còn update được dist thì đánh dấu lại
# if dist[cx] != INF and dist[cx] + ct < dist[cy]:
# dist[cy] = -INF # dist gán bằng âm vô cùng --> bị ảnh hưởng bởi chu trình âm
# return dist
# while True:
# n, m, q, s = list(map(int, input().split()))
# if n == 0 and m == 0 and q == 0 and s == 0:
# break
# edge_list = []
# for _ in range(m):
# u, v, w = list(map(int, input().split()))
# edge_list.append((u, v, w))
# queries = []
# for _ in range(q):
# query = int(input())
# queries.append(query)
# dist = bell_man_ford(s, m, n, edge_list)
# for query in queries:
# if dist[query] == -INF:
# print('-Infinity')
# elif dist[query] == INF:
# print('Impossible')
# else:
# print(dist[query])
# print()
# # Monk's Business Day
# INF = int(1e9)
# def bell_man_ford(s, n, edge_list):
# dist = [INF for index in range(n + 1)]
# dist[s] = 0
# for i in range(n):
# for edge in edge_list:
# cx, cy, cw = edge
# if dist[cx] != INF and dist[cx] + cw < dist[cy]:
# dist[cy] = dist[cx] + cw
# if i == n - 1:
# return True
# return False
# t = int(input())
# for _ in range(t):
# n, m = list(map(int, input().split()))
# edge_list = []
# for i in range(m):
# u, v, w = list(map(int, input().split()))
# edge_list.append((u, v, -w))
# result = bell_man_ford(1, n, edge_list)
# if result:
# print('Yes')
# else:
# print('No')
# Alice in Amsterdam, I mean Wonderland
INF = (2 ** 30) * 100 + 7
def bell_man_ford(s):
dist[s][s] = 0
for i in range(n - 1):
for edge in graph:
u, v, w = edge
if dist[s][u] != INF and dist[s][u] + w < dist[s][v]:
dist[s][v] = dist[s][u] + w
for i in range(n - 1):
for edge in graph:
u, v, w = edge
if dist[s][u] != INF and dist[s][u] + w < dist[s][v]:
dist[s][v] = -INF
tc = 1
while True:
n = int(input())
if n == 0:
break
monuments = []
graph = []
dist = [[INF for i in range(n)] for i in range(n)]
for i in range(n):
name, *weights = input().split()
monuments.append(name)
for j in range(n):
w = int(weights[j])
if i != j and w == 0:
continue
if i == j and w >= 0:
w = 0
graph.append((i, j, w))
for i in range(n):
bell_man_ford(i)
print('Case #{}:'.format(tc))
tc += 1
q = int(input())
for _ in range(q):
u, v = map(int, input().split())
if dist[u][v] <= -INF:
print('NEGATIVE CYCLE')
elif dist[u][v] == INF:
print('{}-{} {}'.format(monuments[u], monuments[v], 'NOT REACHABLE'))
else:
print('{}-{} {}'.format(monuments[u], monuments[v], dist[u][v]))
# # Maelstrom
# INF = int(1e9)
# def bell_man_ford(s, n, dist, graph):
# dist[s] = 0
# for i in range(n - 1):
# for edge in graph:
# u, v, w = edge
# dist[v] = min(dist[v], dist[u] + w)
# n = int(input())
# dist = [INF for index in range(n + 1)]
# graph = []
# for i in range(2, n + 1):
# line = input().split()
# for j in range(1, i):
# if line[j - 1] != 'x':
# w = int(line[j - 1])
# graph.append((i, j, w))
# graph.append((j, i, w))
# bell_man_ford(1, n, dist, graph)
# res = 0
# for i in range(1, n + 1):
# res = max(res, dist[i])
# print(res)