This page is still under construction.

Parts of this page are still being built. What you see may change.

Homing

Time limit1sMemory limit128 MB

Summary
Given a weighted graph and a fixed shortest route, find the smallest fuel load that still brings the driver home when one road on the route is blocked.
Level

Hard8 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

Mr. Kim visits his hometown every year. He always drives along the shortest path to get there, and because he is very economical, he fills the tank with exactly the amount of gasoline needed for that shortest path. Last year an unexpected accident blocked a road on his shortest path, forced him onto a detour, and he ran out of gasoline before reaching home.

This year he wants to carry enough gasoline to survive one unexpected accident. Statistics say that at most one accident happens per day, and his hometown is always reachable within a single day of driving. So he wants to fill the tank with the smallest amount of gasoline that still lets him get home even if exactly one accident occurs somewhere on his shortest path. Whenever an accident happens, he recomputes the shortest remaining route from wherever he is standing.

The road network is a weighted graph G=(V,E)G = (V, E), where VV is the set of cities, EE is the set of roads, and the weight w(e)w(e) of a road ee is the amount of gasoline needed to drive it. An accident always happens in the middle of a road, and Mr. Kim only learns about it when he arrives at one of the two cities that the road connects; accidents are never announced in advance.

Concretely, suppose an accident lies on a road of his shortest path, between the city he is about to leave and the next city on the path. At that moment he is standing at the earlier city, the gasoline used to get there is already spent, and he must reach home along the shortest route that does not use the blocked road.

For example, suppose the departure city is node 0 and the destination is node 5 in the figure below. The shortest path is P=⟨0,1,4,5⟩P = \langle 0, 1, 4, 5 \rangle with total weight W(P)=6W(P) = 6. If the accident blocks road (0,1)(0, 1), the shortest detour from node 0 that avoids it is ⟨0,2,4,5⟩\langle 0, 2, 4, 5 \rangle, which costs 2 more units than PP. If it blocks road (1,4)(1, 4), the detour from node 1 is ⟨1,3,5⟩\langle 1, 3, 5 \rangle, costing 0 extra units. If it blocks road (4,5)(4, 5), the detour from node 4 is ⟨4,1,3,5⟩\langle 4, 1, 3, 5 \rangle, costing 4 extra units. The worst case needs 4 units on top of W(P)W(P), so Mr. Kim must carry at least 10 units of gasoline.

Given the road network and the shortest path that Mr. Kim follows, find the smallest amount of gasoline he must carry so that he can always get home despite one accident.

Input

The first line contains the number of test cases TT (1≤T≤20)(1 \le T \le 20).

Each test case begins with a line containing two integers nn and mm (3≤n,m≤10000)(3 \le n, m \le 10000), the number of cities and the number of roads. Cities are numbered from 00 to n−1n - 1. Each of the next mm lines contains three integers cc, dd, and ww, describing a two-way road between distinct cities cc and dd (c≠d)(c \ne d) that needs ww units of gasoline to drive.

The next line describes Mr. Kim's shortest path. It starts with an integer kk, the number of cities on the path, followed by those kk cities in order. The first is the departure city and the last is the destination.

Output

For each test case, print a single line with the smallest amount of gasoline Mr. Kim must carry. If some accident could leave him with no way to reach home, print −1-1 for that test case instead.

Examples4

  1. Example 1

    Input
    2
    6 8
    0 1 1
    0 2 3
    2 1 1
    4 2 2
    1 4 2
    5 4 3
    3 1 3
    5 3 2
    4 0 1 4 5
    4 3
    0 1 2
    2 1 4
    1 3 3
    3 0 1 3
    
    Expected output
    10
    -1
    
  2. Example 2

    Input
    1
    3 3
    0 1 1
    1 2 1
    0 2 5
    3 0 1 2
    
    Expected output
    7
    
  3. Example 3

    Input
    1
    4 4
    0 1 2
    1 2 2
    2 3 2
    0 2 10
    4 0 1 2 3
    
    Expected output
    -1
    
  4. Example 4

    Input
    1
    3 3
    0 1 5
    0 2 3
    2 1 3
    2 0 1
    
    Expected output
    6