from heapq import *
from bisect import bisect_right
maxint = int(2E9)
class Node(object):
def __init__(self, u, dist):
self.u = u
self.dist = dist
def __cmp__(self, other):
return cmp(self.dist, other.dist)
def binary_search(L, val):
if not L : return False
s = bisect_right(L, val)
return L[s-1] == val
def dijkstra(start):
global N, M, V, B, P
dist = [maxint for i in range(N)]
start = B[0]
dist[start] = 0
Q = []
heappush(Q, Node(start, 0))
while Q :
node = heappop(Q)
u, dist_u = node.u, node.dist
if dist_u > dist[u] : continue
for power in V[u]:
to = u+power
cost = dist_u + 1
while to < N :
if dist[to] > cost :
dist[to] = cost
heappush(Q, Node(to, cost))
if binary_search(V[to], power):
break
to += power
cost += 1
to = u-power
cost = dist_u + 1
while to >= 0 :
if dist[to] > cost :
dist[to] = cost
heappush(Q, Node(to, cost))
if binary_search(V[to], power):
break
to -= power
cost += 1
return dist
def main():
global N, M, V, B, P
N, M = map(int, raw_input().split())
V = [ [] for i in range(N) ]
B = [None for i in range(M)]
P = [None for i in range(M)]
for i in range(M):
B[i], P[i] = map(int, raw_input().split())
V[B[i]].append(P[i])
for i in range(N):
V[i] = list(set(V[i]))
V[i].sort()
start = B[0]
dist = dijkstra(start)
ans = dist[B[1]]
if ans == maxint: print -1
else: print ans
return
main()
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
36 KB |
Output is correct |
2 |
Correct |
33 ms |
36 KB |
Output is correct |
3 |
Correct |
31 ms |
36 KB |
Output is correct |
4 |
Correct |
29 ms |
36 KB |
Output is correct |
5 |
Correct |
29 ms |
36 KB |
Output is correct |
6 |
Correct |
27 ms |
36 KB |
Output is correct |
7 |
Correct |
26 ms |
36 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
36 KB |
Output is correct |
2 |
Correct |
26 ms |
36 KB |
Output is correct |
3 |
Correct |
26 ms |
36 KB |
Output is correct |
4 |
Correct |
31 ms |
36 KB |
Output is correct |
5 |
Correct |
26 ms |
36 KB |
Output is correct |
6 |
Correct |
26 ms |
36 KB |
Output is correct |
7 |
Correct |
30 ms |
36 KB |
Output is correct |
8 |
Correct |
35 ms |
36 KB |
Output is correct |
9 |
Correct |
35 ms |
36 KB |
Output is correct |
10 |
Correct |
49 ms |
38 KB |
Output is correct |
11 |
Correct |
84 ms |
43 KB |
Output is correct |
12 |
Correct |
84 ms |
43 KB |
Output is correct |
13 |
Correct |
89 ms |
43 KB |
Output is correct |
14 |
Correct |
123 ms |
44 KB |
Output is correct |
15 |
Correct |
99 ms |
44 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
44 KB |
Output is correct |
2 |
Correct |
31 ms |
44 KB |
Output is correct |
3 |
Correct |
30 ms |
44 KB |
Output is correct |
4 |
Correct |
35 ms |
44 KB |
Output is correct |
5 |
Correct |
27 ms |
44 KB |
Output is correct |
6 |
Correct |
29 ms |
44 KB |
Output is correct |
7 |
Correct |
29 ms |
44 KB |
Output is correct |
8 |
Correct |
27 ms |
44 KB |
Output is correct |
9 |
Correct |
32 ms |
44 KB |
Output is correct |
10 |
Correct |
42 ms |
44 KB |
Output is correct |
11 |
Correct |
76 ms |
44 KB |
Output is correct |
12 |
Correct |
68 ms |
44 KB |
Output is correct |
13 |
Correct |
78 ms |
44 KB |
Output is correct |
14 |
Correct |
115 ms |
44 KB |
Output is correct |
15 |
Correct |
129 ms |
44 KB |
Output is correct |
16 |
Correct |
43 ms |
44 KB |
Output is correct |
17 |
Correct |
152 ms |
46 KB |
Output is correct |
18 |
Correct |
60 ms |
46 KB |
Output is correct |
19 |
Correct |
54 ms |
46 KB |
Output is correct |
20 |
Correct |
100 ms |
46 KB |
Output is correct |
21 |
Correct |
73 ms |
46 KB |
Output is correct |
22 |
Correct |
51 ms |
46 KB |
Output is correct |
23 |
Correct |
180 ms |
47 KB |
Output is correct |
24 |
Correct |
258 ms |
50 KB |
Output is correct |
25 |
Correct |
254 ms |
50 KB |
Output is correct |
26 |
Correct |
142 ms |
50 KB |
Output is correct |
27 |
Correct |
139 ms |
50 KB |
Output is correct |
28 |
Correct |
111 ms |
50 KB |
Output is correct |
29 |
Correct |
164 ms |
50 KB |
Output is correct |
30 |
Correct |
148 ms |
50 KB |
Output is correct |
31 |
Correct |
191 ms |
50 KB |
Output is correct |
32 |
Correct |
194 ms |
50 KB |
Output is correct |
33 |
Correct |
253 ms |
51 KB |
Output is correct |
34 |
Correct |
244 ms |
52 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
52 KB |
Output is correct |
2 |
Correct |
32 ms |
52 KB |
Output is correct |
3 |
Correct |
32 ms |
52 KB |
Output is correct |
4 |
Correct |
32 ms |
52 KB |
Output is correct |
5 |
Correct |
31 ms |
52 KB |
Output is correct |
6 |
Correct |
27 ms |
52 KB |
Output is correct |
7 |
Correct |
29 ms |
52 KB |
Output is correct |
8 |
Correct |
33 ms |
52 KB |
Output is correct |
9 |
Correct |
29 ms |
52 KB |
Output is correct |
10 |
Correct |
38 ms |
52 KB |
Output is correct |
11 |
Correct |
84 ms |
52 KB |
Output is correct |
12 |
Correct |
71 ms |
52 KB |
Output is correct |
13 |
Correct |
71 ms |
52 KB |
Output is correct |
14 |
Correct |
121 ms |
52 KB |
Output is correct |
15 |
Correct |
100 ms |
52 KB |
Output is correct |
16 |
Correct |
46 ms |
52 KB |
Output is correct |
17 |
Correct |
189 ms |
52 KB |
Output is correct |
18 |
Correct |
52 ms |
52 KB |
Output is correct |
19 |
Correct |
57 ms |
52 KB |
Output is correct |
20 |
Correct |
93 ms |
52 KB |
Output is correct |
21 |
Correct |
85 ms |
52 KB |
Output is correct |
22 |
Correct |
47 ms |
52 KB |
Output is correct |
23 |
Correct |
169 ms |
52 KB |
Output is correct |
24 |
Correct |
222 ms |
52 KB |
Output is correct |
25 |
Correct |
220 ms |
52 KB |
Output is correct |
26 |
Correct |
166 ms |
52 KB |
Output is correct |
27 |
Correct |
139 ms |
52 KB |
Output is correct |
28 |
Correct |
153 ms |
52 KB |
Output is correct |
29 |
Correct |
194 ms |
52 KB |
Output is correct |
30 |
Correct |
145 ms |
52 KB |
Output is correct |
31 |
Correct |
154 ms |
52 KB |
Output is correct |
32 |
Correct |
174 ms |
52 KB |
Output is correct |
33 |
Correct |
264 ms |
52 KB |
Output is correct |
34 |
Correct |
198 ms |
52 KB |
Output is correct |
35 |
Correct |
274 ms |
53 KB |
Output is correct |
36 |
Correct |
227 ms |
53 KB |
Output is correct |
37 |
Correct |
313 ms |
53 KB |
Output is correct |
38 |
Correct |
311 ms |
56 KB |
Output is correct |
39 |
Correct |
267 ms |
56 KB |
Output is correct |
40 |
Correct |
329 ms |
56 KB |
Output is correct |
41 |
Correct |
318 ms |
56 KB |
Output is correct |
42 |
Correct |
192 ms |
56 KB |
Output is correct |
43 |
Correct |
186 ms |
56 KB |
Output is correct |
44 |
Correct |
125 ms |
56 KB |
Output is correct |
45 |
Correct |
249 ms |
56 KB |
Output is correct |
46 |
Correct |
254 ms |
57 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
57 KB |
Output is correct |
2 |
Correct |
29 ms |
57 KB |
Output is correct |
3 |
Correct |
27 ms |
57 KB |
Output is correct |
4 |
Correct |
26 ms |
57 KB |
Output is correct |
5 |
Correct |
27 ms |
57 KB |
Output is correct |
6 |
Correct |
27 ms |
57 KB |
Output is correct |
7 |
Correct |
26 ms |
57 KB |
Output is correct |
8 |
Correct |
28 ms |
57 KB |
Output is correct |
9 |
Correct |
31 ms |
57 KB |
Output is correct |
10 |
Correct |
47 ms |
57 KB |
Output is correct |
11 |
Correct |
93 ms |
57 KB |
Output is correct |
12 |
Correct |
79 ms |
57 KB |
Output is correct |
13 |
Correct |
68 ms |
57 KB |
Output is correct |
14 |
Correct |
107 ms |
57 KB |
Output is correct |
15 |
Correct |
97 ms |
57 KB |
Output is correct |
16 |
Correct |
44 ms |
57 KB |
Output is correct |
17 |
Correct |
189 ms |
57 KB |
Output is correct |
18 |
Correct |
53 ms |
57 KB |
Output is correct |
19 |
Correct |
60 ms |
57 KB |
Output is correct |
20 |
Correct |
88 ms |
57 KB |
Output is correct |
21 |
Correct |
82 ms |
57 KB |
Output is correct |
22 |
Correct |
50 ms |
57 KB |
Output is correct |
23 |
Correct |
151 ms |
57 KB |
Output is correct |
24 |
Correct |
202 ms |
57 KB |
Output is correct |
25 |
Correct |
212 ms |
57 KB |
Output is correct |
26 |
Correct |
152 ms |
57 KB |
Output is correct |
27 |
Correct |
168 ms |
57 KB |
Output is correct |
28 |
Correct |
134 ms |
57 KB |
Output is correct |
29 |
Correct |
211 ms |
57 KB |
Output is correct |
30 |
Correct |
141 ms |
57 KB |
Output is correct |
31 |
Correct |
172 ms |
57 KB |
Output is correct |
32 |
Correct |
207 ms |
57 KB |
Output is correct |
33 |
Correct |
279 ms |
57 KB |
Output is correct |
34 |
Correct |
253 ms |
57 KB |
Output is correct |
35 |
Correct |
315 ms |
57 KB |
Output is correct |
36 |
Correct |
241 ms |
57 KB |
Output is correct |
37 |
Correct |
323 ms |
57 KB |
Output is correct |
38 |
Correct |
330 ms |
57 KB |
Output is correct |
39 |
Correct |
327 ms |
57 KB |
Output is correct |
40 |
Correct |
318 ms |
57 KB |
Output is correct |
41 |
Correct |
269 ms |
57 KB |
Output is correct |
42 |
Correct |
153 ms |
57 KB |
Output is correct |
43 |
Correct |
172 ms |
57 KB |
Output is correct |
44 |
Correct |
131 ms |
57 KB |
Output is correct |
45 |
Correct |
233 ms |
57 KB |
Output is correct |
46 |
Correct |
269 ms |
57 KB |
Output is correct |
47 |
Correct |
555 ms |
59 KB |
Output is correct |
48 |
Correct |
114 ms |
59 KB |
Output is correct |
49 |
Correct |
113 ms |
59 KB |
Output is correct |
50 |
Correct |
108 ms |
59 KB |
Output is correct |
51 |
Correct |
786 ms |
63 KB |
Output is correct |
52 |
Correct |
668 ms |
63 KB |
Output is correct |
53 |
Correct |
390 ms |
63 KB |
Output is correct |
54 |
Correct |
163 ms |
63 KB |
Output is correct |
55 |
Correct |
180 ms |
63 KB |
Output is correct |
56 |
Correct |
999 ms |
63 KB |
Output is correct |
57 |
Correct |
756 ms |
70 KB |
Output is correct |
58 |
Correct |
223 ms |
70 KB |
Output is correct |
59 |
Correct |
232 ms |
70 KB |
Output is correct |
60 |
Correct |
256 ms |
70 KB |
Output is correct |
61 |
Correct |
402 ms |
70 KB |
Output is correct |
62 |
Correct |
392 ms |
70 KB |
Output is correct |
63 |
Correct |
694 ms |
70 KB |
Output is correct |
64 |
Correct |
563 ms |
70 KB |
Output is correct |
65 |
Correct |
831 ms |
74 KB |
Output is correct |
66 |
Execution timed out |
1054 ms |
98 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |