Flowery Trails
InterviewTime limit2sMemory limit256 MB
Add twice the length of every trail that lies on some shortest route from point 0 to point P-1.
- Level
Medium4 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
A national park has points of interest and two-way trails that link them. The entrance is point and the highest peak is point .
Visitors walk from the entrance to the highest peak along a shortest path. Different visitors choose different shortest paths, so every shortest path is used by someone. A trail is popular when it lies on at least one shortest path from the entrance to the highest peak.
The park manager plants flowers along both sides of every popular trail, so a popular trail of length metres needs metres of flowers. Compute the total length of flowers needed.
Two points can be linked by more than one trail, and a trail can start and end at the same point. Each trail counts separately, and a popular trail counts once no matter how many shortest paths use it. At least one path from the entrance to the highest peak exists.
Input
The first line has two integers and .
Each of the next lines has three integers , , and , meaning that a two-way trail of length metres links point and point . The two points are not necessarily distinct.
Integers on the same line are separated by a single space.
Output
Print one integer, the total length of flowers in metres.
Limits
- , the number of points.
- , the number of trails.
- , the length of a trail.
- .