Archaeologist Benjamas is running for her life after investigating the mysterious Crocodile's Underground City. The city has chambers. There are bidirectional corridors, each connecting a different pair of distinct chambers. Running through different corridors may require different amounts of time. Only of the chambers are exit chambers that allow her to escape. Benjamas starts in chamber . She wants to reach an exit chamber as quickly as possible.
The Crocodile gatekeeper wants to prevent Benjamas from escaping. From his den, he controls secret doors that can block any single corridor. That is, whenever he blocks a new corridor, the previously blocked one has to be reopened.
Benjamas's situation can be described as follows: Each time she tries to leave a chamber, the Crocodile gatekeeper may choose to block one of the corridors adjacent to it. Benjamas then chooses and follows one of the unblocked corridors to the next chamber. Once Benjamas enters a corridor, the Crocodile gatekeeper may not block it until Benjamas reaches the other end. Once she enters the next chamber, the gatekeeper may again choose to block one of the outgoing corridors (possibly the corridor that Benjamas just followed), and so on.
She would like to have a simple escape plan in advance. More precisely, she would like to have a set of instructions that tell her what to do when she gets to a chamber. Let be one of the chambers. If it is an exit chamber, no instructions are needed–obviously, she can escape the city. Otherwise, the instruction for chamber should have one of the following forms:
- "If you ever reach chamber , take the corridor leading to chamber . However, if that corridor is blocked, then take the corridor leading to chamber ."
- "Don't bother about chamber ; according to this escape plan you cannot possibly reach it."
Note that in some cases (for example, if your plan directs Benjamas to run in a cycle) the gatekeeper may be able to prevent Benjamas from reaching an exit. An escape plan is good if Benjamas is guaranteed to reach an exit chamber after a finite amount of time, regardless of what the gatekeeper does. For a good escape plan, let be the smallest time such that after time , Benjamas is guaranteed to reach an exit. In that case, we say that the good escape plan takes time .
Your task
Write a program that takes the following parameters:
- – the number of chambers. The chambers are numbered through .
- – the number of corridors. The corridors are numbered through .
- – a two-dimensional array of integers representing the corridors. For , corridor connects two distinct chambers and . No two corridors join the same pair of chambers.
- – a one-dimensional array of integers containing the times needed to traverse the corridors. For , the value is the time Benjamas needs to run through the corridor.
- – the number of exit chambers. You may assume that .
- – a one-dimensional array of integers with distinct entries describing the exit chambers. For , the value is the number of the exit chamber. Chamber will never be one of the exit chambers.
Your program must output the smallest time for which there exists a good escape plan that takes time .
You may assume that each non-exit chamber will have at least two corridors leaving it. You may also assume that in each test case there is a good escape plan for which .
Examples
Example 1
Consider the case shown in Figure 1, where , , , and
Chambers are shown as circles, and corridors connecting them are shown as lines. Exit chambers are shown as thick-bordered circles. Benjamas starts at chamber (marked by a triangle). An optimal escape plan is the following one:
- If you ever reach chamber , take the corridor leading to chamber . However, if that corridor is blocked, then take the corridor leading to chamber .
- If you ever reach chamber , take the corridor leading to chamber . However, if that corridor is blocked, then take the corridor leading to chamber .
In the worst case, Benjamas will reach an exit chamber in 7 units of time. Hence, your program should output .
Example 2
Consider the case shown in Figure 2, where , , , and
Here is an optimal escape plan:
- If you ever reach chamber , take the corridor leading to chamber . However, if that corridor is blocked, then take the corridor leading to chamber .
- If you ever reach chamber , take the corridor leading to chamber . However, if that corridor is blocked, then take the corridor leading to chamber .
- Don't bother about chamber ; according to this escape plan you cannot possibly reach it.
Benjamas will reach one of the exit chambers no later than after units of time. Therefore, your program should output .
Input Specification
- Line : , , and
- Lines to : For , line contains , , and , separated by a space.
- Line : a list of integers , separated by a space.
Output Specification
One integer - the expected solution.
Sample Input 1
5 4 3
0 1 2
0 2 3
3 2 1
2 4 4
1 3 4
Sample Output 1
7
Sample Input 2
5 7 2
0 2 4
0 3 3
3 2 2
2 1 10
0 1 100
0 4 7
3 4 9
1 3
Sample Output 2
14
Subtasks
Subtask 1 (46 points)
- The underground city is a tree. That is, and for each pair of chambers and there is a sequence of corridors connecting and .
- Every exit chamber is connected to exactly one other chamber.
- Any other chamber is connected directly to three or more other chambers.
Comments