UVAOJ301

Written by    20:37 March 4, 2015 

UVAOJ301

 

301 – Transportation

Time limit: 3.000 seconds

 Transportation 

Ruratania is just entering capitalism and is establishing new enterprising activities in many fields including transport. The transportation company TransRuratania is starting a new express train from city A to city B with several stops in the stations on the way. The stations are successively numbered, city A station has number 0, city B station number m. The company runs an experiment in order to improve passenger transportation capacity and thus to increase its earnings. The train has a maximum capacity n passengers. The price of the train ticket is equal to the number of stops (stations) between the starting station and the destination station (including the destination station). Before the train starts its route from the city A, ticket orders are collected from all onroute stations. The ticket order from the station S means all reservations of tickets from S to a fixed destination station. In case the company cannot accept all orders because of the passenger capacity limitations, its rejection policy is that it either completely accept or completely reject single orders from single stations.

Write a program which for the given list of orders from single stations on the way from A to B determines the biggest possible total earning of the TransRuratania company. The earning from one accepted order is the product of the number of passengers included in the order and the price of their train tickets. The total earning is the sum of the earnings from all accepted orders.

Input

The input file is divided into blocks. The first line in each block contains three integers: passenger capacity n of the train, the number of the city B station and the number of ticket orders from all stations. The next lines contain the ticket orders. Each ticket order consists of three integers: starting station, destination station, number of passengers. In one block there can be maximum 22 orders. The number of the city B station will be at most 7. The block where all three numbers in the first line are equal to zero denotes the end of the input file.

Output

The output file consists of lines corresponding to the blocks of the input file except the terminating block. Each such line contains the biggest possible total earning.

Sample Input

Sample Output

关于题意的话要注意对于单张订单,只能全部接受所有乘客或者全部拒绝所有乘客,比如

这张订单,如果要接受的话就必须搭载7位乘客,而不能只搭载其中一部分,这样的话就比较好处理一点。

然后回溯暴力的时候不能太暴力了,从第一张订单开始一张一张往下DFS,每次DFS只用管当前订单以下的。

最后坑爹的是每个输入的开始三个数即火车最大载客量和B站个数以及订单个数是可能部分为零的,以前类似的情况都是习惯while(… a && b && c),搞笑的是之前应该是题目的数据也没太在意这个地方所以基本没出问题,这次乖乖吃了两个WA 囧。

 

Category : acmstudy

Tags :