Treasure Hunt
InterviewTime limit4sMemory limit1024 MB
For each island x, find the maximum of v_y minus the cheapest sea route cost from x to any island y.
- Level
Medium5 of 10
- Topics
- Graph, Shortest path
- Solved
- No attempts yet
Problem
Perry the Pirate is sailing the seven seas! He has a map consisting of islands connected by a network of sea routes. The -th sea route connects islands and and costs coins to traverse in either direction. Fighting off sea monsters is expensive. Perry has scouted each of the islands and found that the -th island has a treasure chest with coins inside.
Next, he plans his journey. He sails along some (possibly empty) path of sea routes that starts at island and ends at island . At the end, he opens the chest at island and collects the booty.
There is one problem. Perry does not know which island he is on. For every possible starting island , he wants the maximum number of coins he can earn over all journeys starting at island . You may assume Perry has enough coins to traverse any path he chooses. He only cares about the net profit of the journey.
Input
The first line contains two space-separated integers and .
The second line contains space-separated integers ().
The next lines each contain three space-separated integers , (), and (). There is at most one sea route between any pair of islands, and each sea route connects two distinct islands.
Output
Output lines. The -th line contains the maximum net profit (in coins) of any journey starting at island .