Tickets
Time limit2sMemory limit1024 MB
For each starting checkpoint, find the minimum total ticket price needed to reach both checkpoint 1 and checkpoint N, or -1 if impossible.
- Level
Hard8 of 10
- Topics
- Shortest path, Segment tree, Graph
- Solved
- No attempts yet
Problem
Bessie is going on a hiking excursion. The trail she is currently traversing consists of checkpoints labeled ().
There are tickets available for purchase (). The -th ticket can be purchased at checkpoint () for price () and provides access to all checkpoints in (). Before entering any checkpoint, Bessie must have purchased a ticket that allows access to that checkpoint. Once Bessie has access to a checkpoint, she may return to it at any point in the future. She may travel between any two checkpoints to which she has access, whether or not their labels differ by 1.
For each , suppose Bessie initially has access to only checkpoint . Output the minimum total price required to purchase access to both checkpoints and . If it is impossible, output -1.
Input
The first line contains and .
Each of the next lines contains four integers , , , and .
Output
Output lines, one for each checkpoint.