Mimi is attending a Thanksgiving feast! She lives in a town with
buildings numbered from
to
and
unweighted bidirectional roads connecting them. The
road connects buildings
and
. Mimi is a good guest, so she wants to bring a gift for the host. However, Mimi is also a procrastinator! The Thanksgiving feast is soon and she hasn't bought a gift yet. There are
buildings
which sell gifts. Mimi is currently at building
and the feast is at building
. She needs to visit at least one of the
buildings and go to the feast. Help her find the length of the shortest way to do so!
It is guaranteed that there exists a way. Mimi is allowed to pass through
before buying a gift.
Constraints



Subtask 1 [30%]



Subtask 2 [30%]



Subtask 3 [40%]



Input Specification
The first line of input will contain five space-separated integers
,
,
,
,
.
The next line will contain
space-separated integers
representing the buildings which sell gifts.
The next
lines each contain two space-separated integers,
and
.
Output Specification
Output a single integer, the shortest distance.
Sample Input 1
Copy
5 4 3 1 2
3 4 5
1 2
2 3
3 4
4 5
Sample Output 1
Copy
3
Explanation for Sample 1
Mimi starts at building 1. She passes by
, to head to
. She then returns to
, giving a total length of
.
Sample Input 2
Copy
5 5 1 1 2
4
1 2
2 3
3 4
4 5
5 1
Sample Output 2
Copy
4
Comments
Does anyone mind taking a look at my code? I have WAs from each batch but I can't think of any case that would break my code. What I did was do BFS from the start and end points simultaneously until I reach the first store that both sides have already reached, in which case, I exit the program. Thanks in advance!
I dont think you should to the bfs simultaneously, rather you should do it separately.
Thank you for the feedback! But quick question, why is it faulty to do BFS from both sides simultaneously? I changed my code as you suggested and it passed.