#P7984. [USACO21DEC] Tickets P
[USACO21DEC] Tickets P
Problem Description
Bessie is going on a hiking trip! The route she is currently traveling consists of checkpoints numbered ().
There are () tickets available for purchase. Ticket can be bought at checkpoint () for price (), and it allows entry to all checkpoints in (). Before entering any checkpoint, Bessie must have already bought a ticket that allows her to enter that checkpoint. Once Bessie can travel to a checkpoint, she can return to that checkpoint at any time in the future.
For each , if Bessie can initially only enter checkpoint , output the minimum total cost required to be able to enter checkpoints and . If it is impossible, output .
Input Format
The first line contains and .
The next lines each describe one ticket. For each , line contains four integers , , , and .
Output Format
Output lines. On each line, output the answer for one starting checkpoint.
7 6
4 1 2 3
4 10 5 6
2 100 7 7
6 1000 1 1
5 10000 1 4
6 100000 5 6
-1
-1
-1
1111
10100
110100
-1
Hint
[Sample Explanation]
If Bessie starts from checkpoint , then one way to buy tickets to be able to enter checkpoints and is:
Buy the first ticket at checkpoint , allowing Bessie to enter checkpoints and .
Buy the third ticket at checkpoint , allowing Bessie to enter checkpoint .
Go back to checkpoint and buy the second ticket, allowing Bessie to enter checkpoints and .
Buy the fourth ticket at checkpoint , allowing Bessie to enter checkpoint .
[Constraints]
- Test cases 1-7 satisfy .
- Test cases 8-19 have no additional constraints.
Translated by ChatGPT 5