Roadblock
InterviewTime limit1sMemory limit128 MB
Given a weighted undirected graph, double the length of one edge to maximize the increase in the shortest path from field 1 to field N.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Brute force, Greedy
- Solved
- No attempts yet
Problem
Every morning, Farmer John (FJ) walks across his farm from his house to the barn. The farm has fields () connected by bidirectional paths (), each with a positive length. FJ's house is in field and the barn is in field . No two fields are joined by more than one path, and every field is reachable from every other field. Whenever FJ travels, he always takes a route whose total length is minimum.
FJ's mischievous cows want to disrupt his morning walk. They will pile hay bales on exactly one of the paths, doubling that path's length. The cows want to choose the path so as to maximize the increase in the length of FJ's shortest route from the house to the barn. Determine the largest increase they can cause.
Input
- Line 1: Two space-separated integers and .
- Lines : Line describes the -th bidirectional path with three space-separated integers , , . Fields and (each in ) are joined by a path of length ().
Output
- Line 1: The maximum possible increase in the length of FJ's shortest route from field to field that can be achieved by doubling the length of a single path.
Hint
In the sample there are fields and paths. Initially the shortest route from the house (field ) to the barn (field ) is with total length .
If the cows double the length of the path between fields and (from to ), FJ's shortest route becomes with total length , which is longer than before. No single path can force a larger increase, so the answer is .