The Way to Bytemountain
Time limit1sMemory limit32 MB
Find a walk from crossing 1 to crossing n that deviates from the signpost arrows at most k times and maximizes total trail beauty.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, Binary search, Greedy
- Solved
- No attempts yet
Problem
Byteman is staying at a mountain hostel and wants to climb to the peak of Bytemountain. The mountain has trail crossings, numbered from to . The hostel is at crossing and the peak is crossing .
At every crossing stands a single signpost that points along exactly one of the trails leaving that crossing. Normally every signpost points toward the peak, but the signposts are currently being reorganized, so a signpost may point along any trail — even the signpost at the peak points along some trail.
A guide gives directions of the following form. Starting at the hostel, keep following the trails indicated by the signposts until you reach crossing ; there, ignore the signpost and instead take the trail joining and . Then follow the signposts again until you reach and take the trail joining and , and so on. After the -th such step of consulting the map (taking a chosen trail instead of the signpost trail) at crossing , keep following the signposts and you will eventually arrive at the peak.
Byteman does not want the directions to be too complicated, so he asks the guide to consult the map at most times (that is, to take at most trails that differ from the signpost trails).
The route may pass through the same trail or crossing several times. Byteman's walk ends the first time he reaches the peak after all of the guide's instructions have been carried out; he may pass through the peak earlier without stopping.
Every trail has a beauty value. Find the maximum possible total beauty of the trails traversed on a route from the hostel to the peak that consults the map at most times.
Input
The first line contains two integers and (, ): the number of crossings and the maximum number of times Byteman may consult the map. Crossings are numbered from to ; the hostel is crossing and the peak is crossing .
Each of the next lines describes one crossing. The -th of these lines begins with an integer (), the number of trails leaving crossing , followed by pairs (, ), each meaning that a trail leads from crossing to crossing and has beauty . The first pair on each line is the trail that the signpost at crossing points to.
Every trail is bidirectional and joins two different crossings, and any two crossings are joined by at most one trail. Each trail appears twice in the input, once in the list of each of its two endpoints, with the same beauty both times. The total number of trails does not exceed .
Output
Print a single integer: the maximum total beauty of the trails on a valid route from the hostel to the peak that consults the map at most times. Such a route is guaranteed to exist.
Hint

In the figure, the edges are trails joining crossings, the numbers next to the edges are the beauties of the trails, and the arrows show the trail that each signpost points to.
Consulting the map twice, at crossings and , yields the route with total beauty .