Cow Toll Paths
Time limit1sMemory limit128 MB
For each query, find the cheapest s-t trip where cost is the sum of edge tolls plus the single largest pasture toll on the route. N=250, K=10000.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
Farmer John is always looking for ways to increase his revenue, so he has set up tolls that the cows must pay whenever they walk along the paths of his farm.
The farm has pastures (), numbered through . They are connected by bidirectional paths (); path joins two different pastures and () and has an edge toll (). Two pastures may be joined by more than one path, but no path connects a pasture to itself. Every pasture can be reached from every other pasture, so the graph is connected.
In addition, each pasture has its own toll (). The cost of a trip from one pasture to a different pasture is the sum of the edge tolls of all paths traversed, plus a single additional toll equal to the maximum pasture toll among all pastures visited on the trip, including the starting and ending pastures.
The cows want to compare their options. Answer queries (). Query gives a starting pasture and an ending pasture (); output the minimum possible cost of a trip from to .
Worked example. Consider five pastures with pasture tolls , , , , , connected by paths with edge tolls , , , , , , , where means the path between pastures and has edge toll .
To travel from pasture to pasture , take : the edge tolls sum to and the largest pasture toll on the route is (pasture ), so the total cost is .
To travel from pasture to pasture , take : the edge tolls sum to and the largest pasture toll on the route is (pasture ), so the total cost is .
Input
- The first line contains three space-separated integers , , and .
- Each of the next lines contains one integer; the -th of them is , the toll of pasture .
- Each of the next lines contains three space-separated integers , , and , describing a bidirectional path between pastures and with edge toll .
- Each of the next lines contains two space-separated integers and , giving one query.
Output
- Output lines. The -th line contains a single integer: the minimum possible cost of a trip from to .