Longest Common Path
Time limit1sMemory limit256 MB
Two friends each walk a shortest route from school to their own home, and the goal is to maximize the shared consecutive stretch of both routes.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Dynamic programming
- Solved
- No attempts yet
Problem
Per and Pal are friends and spend a lot of time together. Both are impatient, so whenever they go somewhere they take a fastest route.
School is over and they have to go home. Each one wants to get home as early as possible, and they also want to walk together for as long as they can.
The neighbourhood is a graph whose nodes are intersections and whose edges are roads. Every road is bidirectional and takes the same time in either direction. The school and the two homes sit at intersections, and the three positions are different. Some path connects every pair of intersections.
Per and Pal leave school at the same moment, and each one picks a shortest route to his own home. Choose the two routes so that the total travel time of a run of consecutive roads contained in both routes is as large as possible. Write a program that computes that time.
Input
The first line contains one integer , the number of test cases.
The first line of each test case contains two integers and , the number of intersections and the number of bidirectional roads. The second line contains three integers , and , the intersections of the school, Per's home and Pal's home. Each of the next lines contains three integers , and , meaning that a bidirectional road joins intersections and and takes minutes to traverse. Intersections are numbered from to .
- , and , , are pairwise distinct
- At most one road joins any pair of intersections, and every intersection is reachable from every other one
Output
For each test case, print on one line the length of the longest stretch Per and Pal can walk together while both still reach their own homes as fast as possible. Print when no road lies on both routes.