Homing
Time limit1sMemory limit128 MB
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 , where is the set of cities, is the set of roads, and the weight of a road 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 with total weight . If the accident blocks road , the shortest detour from node 0 that avoids it is , which costs 2 more units than . If it blocks road , the detour from node 1 is , costing 0 extra units. If it blocks road , the detour from node 4 is , costing 4 extra units. The worst case needs 4 units on top of , 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 .
Each test case begins with a line containing two integers and , the number of cities and the number of roads. Cities are numbered from to . Each of the next lines contains three integers , , and , describing a two-way road between distinct cities and that needs units of gasoline to drive.
The next line describes Mr. Kim's shortest path. It starts with an integer , the number of cities on the path, followed by those 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 for that test case instead.