Transportation
Time limit1sMemory limit128 MB
Choose a subset of passenger orders so that no route segment exceeds capacity n, maximizing total revenue.
- Level
Medium5 of 10
- Topics
- Backtracking, Brute force
- Solved
- No attempts yet
Problem
An express train runs from station A to station B, stopping at several stations along the way. The stations are numbered in order: station A is number and station B is number . The train can carry at most passengers, and on every segment of the route the total number of passengers on board must never exceed .
The price of a ticket equals the number of stops between the departure station and the destination station, counting the destination. In other words, a ticket from station to station costs .
Before the train leaves station A, reservation orders are collected from the intermediate stations. Each order is a triple (departure station , destination station , number of passengers ), and the company must either accept an order in full or reject it in full. When the capacity does not allow every order to be accepted, carrying only some passengers of an order is not allowed.
If an order is accepted, its passengers travel from to and therefore occupy every segment in between. The revenue from one accepted order is (number of passengers) (ticket price) , and the total revenue is the sum over all accepted orders.
Write a program that, given the list of orders, determines the largest total revenue the company can earn.
Input
The input consists of several blocks. The first line of each block contains three integers , , and : the passenger capacity , the number of station B, and the number of orders. Each of the next lines describes one order as three integers , , : the departure station, the destination station, and the number of passengers.
A block contains at most orders, and the number of station B is at most . A block whose first line contains three zeros marks the end of the input and is not processed.
Output
For each block except the terminating one, print on its own line the largest total revenue that can be earned.