Paper Route
InterviewTime limit1sMemory limit128 MB
With N+1 nodes and exactly N roads, find the cheapest closed walk from node 0 covering all addresses, then add the campus travel cost from wherever you end.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Dynamic programming, Tree
- Solved
- No attempts yet
Problem
As a cash-strapped student, you take a part-time job as a paper carrier. You are handed a paper route: a set of addresses conveniently labelled to .
Every morning you start at the newspaper office, which is address number . You must plan a route that delivers a newspaper to every address, and you also want to get to class right after you finish. There are exactly roads in your area connecting the addresses, and each takes a known time to traverse. You have also precomputed the time it takes to reach the campus from each location, including the newspaper office. How soon can you finish delivering and be in your seat at school?
Input
The first line contains a single integer (the number of addresses, ).
The next lines each contain an integer (for , with ): the time it takes to get from location to campus.
The final lines each contain three integers , , (, , ), describing a road between locations and that takes minutes to traverse.
You are guaranteed to be able to reach every address. (Recall that location is the newspaper office.)
Output
Output the minimum time it takes to deliver all the papers and get to class.
Hint
It can be better to visit all the addresses, return to the office, and head to school from there.
For example, one route is , taking .