Almost Shortest Path - weighted
Finding the shortest path that goes from a starting point to a destination point given a set of points and route lengths connecting them is an already well known problem, and it's even part of our daily lives, as shortest path programs are widely available nowadays.
Most people usually like very much these applications as they make their lives easier. Well, maybe not that much easier.
Now that almost everyone can have access to GPS navigation devices able to calculate shortest paths, most routes that form the shortest path are getting slower because of heavy traffic. As most people try to follow the same path, it's not worth it anymore to follow these directions.
With this in his mind, your boss asks you to develop a new application that only he will have access to, thus saving him time whenever he has a meeting or any urgent event. He asks you that the program must answer not the shortest path, but the almost shortest path. He defines the almost shortest path as the shortest path that goes from a starting point to a destination point such that no route between two consecutive points belongs to any shortest path from the starting point to the destination.
For example, suppose the figure below represents the map given, with circles representing location points, and lines representing direct, one-way routes with lengths indicated. The starting point is marked as and the destination point is marked as . The bold lines belong to a shortest path (in this case there are two shortest paths, each with total length ). Thus, the almost shortest path would be the one indicated by dashed lines (total length ), as no route between two consecutive points belongs to any shortest path. Notice that there could exist more than one possible answer, for instance if the route with length had length . There could exist no possible answer as well.
Input
Contains several test cases. The first line of a test case contains two integers and , indicating respectively the number of points in the map and the number of existing one-way routes connecting two points directly. Each point is identified by an integer between and . The second line contains two integers and , indicating respectively the starting and the destination points .
Each one of the following lines contains three integers and , indicating the existence of a one-way route from to with distance . There is at most one route from a given point to a given point , but notice that the existence of a route from to does not imply there is a route from to , and, if such road exists, it can have a different length. The last line contains two zeros and will not be processed.
Output
For each test case print a single line, containing if it is not possible to match the requirements, or an integer representing the length of the almost shortest path found.