Route Detour
Time limit2sMemory limit512 MB
Find the cheapest route from a repaired vehicle's city to the destination, where any first contact with the fixed route forces the rest of the trip to follow that route exactly.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Greedy, Implementation
- Solved
- No attempts yet
Problem
The road system of a country links all of its cities, so from any city you can reach every other city over the existing roads. Each road joins two distinct cities, carries traffic in both directions, and has one toll booth. The toll is paid in both directions of travel. Roads meet only at cities. No pair of cities is joined by two or more roads.
Dias Transport runs a parcel delivery service between the cities. Each parcel has to be carried from a city to another city . For every parcel the company fixes a service route made of cities and roads. The first city of the service route is the origin of the parcel and the last one is its destination. The service route never visits the same city twice, and the vehicle assigned to a parcel may drive only along the service route it was given.
One day the vehicle on a delivery broke down. It was taken for repair to a city that is not one of the cities on its service route. The company wants the minimum total toll cost for the vehicle to deliver the parcel to the destination city, starting from the city where it was repaired, under one extra restriction: if at some moment the vehicle passes through one of the cities of its service route, from that city on it must follow the service route again.
Input
The input contains several test cases.
The first line of a test case has four integers , , and (, , , ), giving the number of cities in the country, the number of roads, the number of cities on the service route, and the city where the vehicle was repaired. Cities are identified by integers from to . The service route is : the origin is , from it goes to , from to , and so on up to the destination .
The next lines describe the road system of the country. Each of those lines describes one road and has three integers , and (, , ), meaning that a road joins cities and with toll cost .
The last test case is followed by a line containing four zeros separated by whitespace.
Output
For each test case print a single line with a single integer , the minimum total toll cost for the vehicle to reach the destination.