class Solution:
def numBusesToDestination(self, routes: List[List[int]], source: int, target: int) -> int:
to_route=defaultdict(set)
for i,r in enumerate(routes):
for j in r:
to_route[j].add(i)
bfs=[(source,0)]
seen=set([source])
for stop,dist in bfs:
if stop==target:return dist
for i in to_route[stop]:
for j in routes[i]:
if j not in seen:
bfs.append((j,dist+1))
seen.add(j)
routes[i]=[]
return -1
if stop==target:return dist 这一行要在开始
routes[i]=[]这个剪枝很有必要