#-*- coding: utf-8 -*-
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 :
#### priority queue is not empty ####
node = heappop(Q)
u, dist_u = node.u, node.dist
if dist_u > dist[u] : continue
for power in V[u]:
#### u에 power 만큼의 힘을 가진 도게가 있음 ####
#### 오른쪽으로 탐색을 진행 ####
to = u+power
cost = dist_u + 1
while to < N : #### 목적지의 빌딩번호가 N보다 커지면 안됨 ####
if dist[to] > cost :
dist[to] = cost
heappush(Q, Node(to, cost))
if binary_search(V[to], power):
"""
V[o]에 p만큼의 힘을 가진 도게가 있으므로,
이 도게가 더 가는 것이나 이미 V[o]에 있는 도게가 더 가는 것이나
equivalent함
따라서, 중복탐색을 하지 않는 것이 더 효율적임
"""
break
to += power
cost += 1
#### 왼쪽으로 탐색을 진행 ####
to = u-power
cost = dist_u + 1
while to >= 0 : #### 목적지의 빌딩번호가 0보다 작아지면 안됨 ####
if dist[to] > cost :
dist[to] = cost
heappush(Q, Node(to, cost))
if binary_search(V[to], power):
"""
V[o]에 p만큼의 가진 도게가 있으므로 이 도게가 더 가는 것이나
V[o]에 있는 도게가 더 가는 것이나 equivalent함
따라서, 중복탐색을 하지 않는 것이 더 효율적임
"""
break
to -= power
cost += 1
return dist
def main():
global N, M, V, B, P
"""
V : VERTICES의 집합; V[i][j]는 i라는 빌딩에서 j라는 power를 가진게 있다는걸 의미함
P : POWER; B : BUILDING;
"""
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 |
18 ms |
2 KB |
Output is correct |
2 |
Correct |
16 ms |
3 KB |
Output is correct |
3 |
Correct |
14 ms |
3 KB |
Output is correct |
4 |
Correct |
16 ms |
3 KB |
Output is correct |
5 |
Correct |
14 ms |
3 KB |
Output is correct |
6 |
Correct |
20 ms |
3 KB |
Output is correct |
7 |
Correct |
14 ms |
3 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
3 KB |
Output is correct |
2 |
Correct |
20 ms |
3 KB |
Output is correct |
3 |
Correct |
14 ms |
3 KB |
Output is correct |
4 |
Correct |
20 ms |
3 KB |
Output is correct |
5 |
Correct |
14 ms |
3 KB |
Output is correct |
6 |
Correct |
16 ms |
3 KB |
Output is correct |
7 |
Correct |
14 ms |
3 KB |
Output is correct |
8 |
Correct |
14 ms |
3 KB |
Output is correct |
9 |
Correct |
18 ms |
3 KB |
Output is correct |
10 |
Correct |
19 ms |
3 KB |
Output is correct |
11 |
Correct |
24 ms |
3 KB |
Output is correct |
12 |
Correct |
22 ms |
3 KB |
Output is correct |
13 |
Correct |
30 ms |
3 KB |
Output is correct |
14 |
Correct |
27 ms |
3 KB |
Output is correct |
15 |
Correct |
30 ms |
3 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
3 KB |
Output is correct |
2 |
Correct |
17 ms |
3 KB |
Output is correct |
3 |
Correct |
13 ms |
3 KB |
Output is correct |
4 |
Correct |
17 ms |
3 KB |
Output is correct |
5 |
Correct |
16 ms |
3 KB |
Output is correct |
6 |
Correct |
16 ms |
3 KB |
Output is correct |
7 |
Correct |
14 ms |
3 KB |
Output is correct |
8 |
Correct |
17 ms |
3 KB |
Output is correct |
9 |
Correct |
18 ms |
3 KB |
Output is correct |
10 |
Correct |
19 ms |
3 KB |
Output is correct |
11 |
Correct |
26 ms |
3 KB |
Output is correct |
12 |
Correct |
22 ms |
3 KB |
Output is correct |
13 |
Correct |
19 ms |
3 KB |
Output is correct |
14 |
Correct |
24 ms |
3 KB |
Output is correct |
15 |
Correct |
27 ms |
3 KB |
Output is correct |
16 |
Correct |
19 ms |
3 KB |
Output is correct |
17 |
Correct |
41 ms |
4 KB |
Output is correct |
18 |
Correct |
22 ms |
4 KB |
Output is correct |
19 |
Correct |
19 ms |
4 KB |
Output is correct |
20 |
Correct |
236 ms |
4 KB |
Output is correct |
21 |
Correct |
21 ms |
4 KB |
Output is correct |
22 |
Correct |
18 ms |
4 KB |
Output is correct |
23 |
Correct |
39 ms |
4 KB |
Output is correct |
24 |
Correct |
70 ms |
4 KB |
Output is correct |
25 |
Correct |
76 ms |
4 KB |
Output is correct |
26 |
Correct |
46 ms |
4 KB |
Output is correct |
27 |
Correct |
37 ms |
4 KB |
Output is correct |
28 |
Correct |
46 ms |
4 KB |
Output is correct |
29 |
Correct |
206 ms |
8 KB |
Output is correct |
30 |
Correct |
86 ms |
8 KB |
Output is correct |
31 |
Correct |
112 ms |
8 KB |
Output is correct |
32 |
Correct |
96 ms |
8 KB |
Output is correct |
33 |
Correct |
306 ms |
10 KB |
Output is correct |
34 |
Correct |
338 ms |
11 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
11 KB |
Output is correct |
2 |
Correct |
15 ms |
11 KB |
Output is correct |
3 |
Correct |
20 ms |
11 KB |
Output is correct |
4 |
Correct |
18 ms |
11 KB |
Output is correct |
5 |
Correct |
14 ms |
11 KB |
Output is correct |
6 |
Correct |
15 ms |
11 KB |
Output is correct |
7 |
Correct |
21 ms |
11 KB |
Output is correct |
8 |
Correct |
16 ms |
11 KB |
Output is correct |
9 |
Correct |
20 ms |
11 KB |
Output is correct |
10 |
Correct |
17 ms |
11 KB |
Output is correct |
11 |
Correct |
25 ms |
11 KB |
Output is correct |
12 |
Correct |
19 ms |
11 KB |
Output is correct |
13 |
Correct |
19 ms |
11 KB |
Output is correct |
14 |
Correct |
35 ms |
11 KB |
Output is correct |
15 |
Correct |
27 ms |
11 KB |
Output is correct |
16 |
Correct |
16 ms |
11 KB |
Output is correct |
17 |
Correct |
40 ms |
11 KB |
Output is correct |
18 |
Correct |
19 ms |
11 KB |
Output is correct |
19 |
Correct |
19 ms |
11 KB |
Output is correct |
20 |
Correct |
231 ms |
11 KB |
Output is correct |
21 |
Correct |
24 ms |
11 KB |
Output is correct |
22 |
Correct |
24 ms |
11 KB |
Output is correct |
23 |
Correct |
39 ms |
11 KB |
Output is correct |
24 |
Correct |
63 ms |
11 KB |
Output is correct |
25 |
Correct |
66 ms |
11 KB |
Output is correct |
26 |
Correct |
41 ms |
11 KB |
Output is correct |
27 |
Correct |
37 ms |
11 KB |
Output is correct |
28 |
Correct |
33 ms |
11 KB |
Output is correct |
29 |
Correct |
194 ms |
11 KB |
Output is correct |
30 |
Correct |
85 ms |
11 KB |
Output is correct |
31 |
Correct |
113 ms |
11 KB |
Output is correct |
32 |
Correct |
82 ms |
11 KB |
Output is correct |
33 |
Correct |
329 ms |
11 KB |
Output is correct |
34 |
Correct |
332 ms |
11 KB |
Output is correct |
35 |
Correct |
168 ms |
11 KB |
Output is correct |
36 |
Correct |
62 ms |
11 KB |
Output is correct |
37 |
Correct |
180 ms |
11 KB |
Output is correct |
38 |
Correct |
186 ms |
11 KB |
Output is correct |
39 |
Correct |
219 ms |
11 KB |
Output is correct |
40 |
Correct |
249 ms |
11 KB |
Output is correct |
41 |
Correct |
206 ms |
11 KB |
Output is correct |
42 |
Correct |
119 ms |
11 KB |
Output is correct |
43 |
Correct |
115 ms |
11 KB |
Output is correct |
44 |
Correct |
335 ms |
11 KB |
Output is correct |
45 |
Correct |
455 ms |
14 KB |
Output is correct |
46 |
Correct |
471 ms |
14 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
14 KB |
Output is correct |
2 |
Correct |
15 ms |
14 KB |
Output is correct |
3 |
Correct |
14 ms |
14 KB |
Output is correct |
4 |
Correct |
14 ms |
14 KB |
Output is correct |
5 |
Correct |
16 ms |
14 KB |
Output is correct |
6 |
Correct |
16 ms |
14 KB |
Output is correct |
7 |
Correct |
17 ms |
14 KB |
Output is correct |
8 |
Correct |
19 ms |
14 KB |
Output is correct |
9 |
Correct |
15 ms |
14 KB |
Output is correct |
10 |
Correct |
19 ms |
14 KB |
Output is correct |
11 |
Correct |
21 ms |
14 KB |
Output is correct |
12 |
Correct |
22 ms |
14 KB |
Output is correct |
13 |
Correct |
19 ms |
14 KB |
Output is correct |
14 |
Correct |
27 ms |
14 KB |
Output is correct |
15 |
Correct |
32 ms |
14 KB |
Output is correct |
16 |
Correct |
17 ms |
14 KB |
Output is correct |
17 |
Correct |
39 ms |
14 KB |
Output is correct |
18 |
Correct |
23 ms |
14 KB |
Output is correct |
19 |
Correct |
20 ms |
14 KB |
Output is correct |
20 |
Correct |
249 ms |
14 KB |
Output is correct |
21 |
Correct |
17 ms |
14 KB |
Output is correct |
22 |
Correct |
17 ms |
14 KB |
Output is correct |
23 |
Correct |
39 ms |
14 KB |
Output is correct |
24 |
Correct |
54 ms |
14 KB |
Output is correct |
25 |
Correct |
70 ms |
14 KB |
Output is correct |
26 |
Correct |
39 ms |
14 KB |
Output is correct |
27 |
Correct |
37 ms |
14 KB |
Output is correct |
28 |
Correct |
28 ms |
14 KB |
Output is correct |
29 |
Correct |
179 ms |
14 KB |
Output is correct |
30 |
Correct |
71 ms |
14 KB |
Output is correct |
31 |
Correct |
111 ms |
14 KB |
Output is correct |
32 |
Correct |
80 ms |
14 KB |
Output is correct |
33 |
Correct |
327 ms |
14 KB |
Output is correct |
34 |
Correct |
296 ms |
14 KB |
Output is correct |
35 |
Correct |
144 ms |
14 KB |
Output is correct |
36 |
Correct |
61 ms |
14 KB |
Output is correct |
37 |
Correct |
159 ms |
14 KB |
Output is correct |
38 |
Correct |
202 ms |
14 KB |
Output is correct |
39 |
Correct |
217 ms |
14 KB |
Output is correct |
40 |
Correct |
218 ms |
14 KB |
Output is correct |
41 |
Correct |
200 ms |
14 KB |
Output is correct |
42 |
Correct |
110 ms |
14 KB |
Output is correct |
43 |
Correct |
133 ms |
14 KB |
Output is correct |
44 |
Correct |
330 ms |
14 KB |
Output is correct |
45 |
Correct |
444 ms |
17 KB |
Output is correct |
46 |
Correct |
443 ms |
17 KB |
Output is correct |
47 |
Correct |
910 ms |
25 KB |
Output is correct |
48 |
Correct |
121 ms |
25 KB |
Output is correct |
49 |
Correct |
106 ms |
25 KB |
Output is correct |
50 |
Correct |
105 ms |
25 KB |
Output is correct |
51 |
Execution timed out |
1067 ms |
40 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |