Safe Travel
Time limit3sMemory limit128 MB
For each pasture i, find the shortest path from pasture 1 to i that avoids the last edge of the unique shortest path to i.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Segment tree
- Solved
- No attempts yet
Problem
Gremlins have infested the farm. These nasty, fairy-like creatures love to harass the cows. Every cow starts at the barn, conveniently located at pasture , and walks to its own field: cow travels from pasture to pasture .
Each gremlin knows the unique shortest route its cow normally takes. Gremlin waits in the middle of the last road of the shortest route from pasture to pasture , hoping to harass cow .
To avoid being harassed, each cow instead picks the fastest route from pasture (the barn) to pasture that does not use that last road of its shortest route. For every cow , compute the shortest possible time of such a route that avoids the road guarded by gremlin .
- Pastures are numbered through , where .
- Roads are numbered through , where . Every road is bidirectional.
- Road connects pastures and and takes time to cross (, , ).
- No two roads connect the same pair of pastures, and no road connects a pasture to itself.
- In every test case, the shortest route from pasture to pasture is unique.
For example, consider the following pastures and roads (numbers in brackets are traversal times):
1--[2]--2-------+
| | |
[2] [1] [3]
| | |
+-------3--[4]--4
Without gremlins, the shortest routes are:
When each gremlin guards the last road of its cow's shortest route, the best routes that avoid those roads are:
Input
- Line 1: Two space-separated integers and .
- Lines 2 to : Three space-separated integers , , and .
Output
- Print lines. Line contains the shortest time to travel from pasture to pasture without using the last road of the shortest route from pasture to pasture . If no such route exists, print alone on that line.