-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaek16928.py
77 lines (74 loc) · 2.37 KB
/
baek16928.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
# from sys import stdin
# from collections import deque
# K = 100
# graph = [[] for i in range(K+1)]
# for i in range(1, K+1):
# for dice in range(1, 6+1):
# if i < K-dice+1:
# graph[i].append(i+dice)
# N, M = map(int, stdin.readline().rstrip().split(" "))
# for _ in range(N):
# x, y = map(int, stdin.readline().rstrip().split(" "))
# graph[x] = [y]
# for _ in range(M):
# u, v = map(int, stdin.readline().rstrip().split(" "))
# graph[u] = [v]
#
# isinqueue = [False for _ in range(K+1)]
# def bfs(i):
# willvisit = deque()
# willvisit.append((i, 0))
# isinqueue[i] = True
# while willvisit:
# now = willvisit.popleft()
# i, depth = now
# if i == K:
# return depth
# else:
# if len(graph[i]) == 1 and graph[i][0] != 100:
# # if not isinqueue[graph[i][0]]:
# willvisit.appendleft((graph[i][0], depth))
# isinqueue[graph[i][0]] = True
# else:
# for node in graph[i]:
# if not isinqueue[node]:
# willvisit.append((node, depth+1))
# isinqueue[node] = True
#
# print(bfs(1))
from sys import stdin
from collections import deque
K = 100
graph = [[] for i in range(K+1)]
for i in range(1, K+1):
for dice in range(1, 6+1):
if i < K-dice+1:
graph[i].append(i+dice)
N, M = map(int, stdin.readline().rstrip().split(" "))
for _ in range(N):
x, y = map(int, stdin.readline().rstrip().split(" "))
graph[x] = [y]
for _ in range(M):
u, v = map(int, stdin.readline().rstrip().split(" "))
graph[u] = [v]
isinqueue = [0 for _ in range(K+1)]
def bfs(i):
willvisit = deque()
willvisit.append((i, 0))
isinqueue[i] = True
while willvisit:
now = willvisit.popleft()
i, depth = now
if i == K:
return depth
else:
if len(graph[i]) == 1 and graph[i][0] != 100:
if not isinqueue[graph[i][0]] or isinqueue[graph[i][0]] > depth :
willvisit.appendleft((graph[i][0], depth))
isinqueue[graph[i][0]] = depth
else:
for node in graph[i]:
if not isinqueue[node]:
willvisit.append((node, depth+1))
isinqueue[node] = depth + 1
print(bfs(1))