UVA - 301 Transportation (回溯)

来源:互联网 发布:suse linux 开放端口 编辑:程序博客网 时间:2024/05/22 06:06


 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 numberm. The company runs an experiment in order to improve passenger transportation capacity and thus to increase its earnings. The train has a maximum capacityn 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 capacityn 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

10 3 40 2 11 3 51 2 72 3 1010 5 43 5 102 4 90 2 52 5 80 0 0

Sample Output

1934


题目大意:
有一家运输公司,运营一段铁路,该铁路A 站到B站。从A站开始到B站编号为0....N-1。每辆火车的限载人数为n人,车票的价钱按站数计算,搭一个站收1元, n个站即n元。为了让收益最大化,每次开车前,都会先分析所有的车票,相同起点和终点的归为同一个订单。因为人数限制,如果人数太多的话,就必须要放弃一些订单。 这个公司的做法有点极端,要么整个订单都放弃,要么整个订单都接受。
现在给你:
第一行3个数分别是最大载客量,车站数n,订单数m。
接下m来一行是该订单的起始点,终点,和该班次的载客量

编写一个程序,输出最大能收入多少钱。

解析:
这题不能用车站来进行判断,应该要按照订单是否被接来进行判断。
我们可以取一个数组sta来记录列车到每一站时最多能容纳的乘客量,每次回溯都判断该数组是否超过了最大载客量,如果当前订单的乘客量小于或等于列车到该站时能容纳的乘客量,则该订单可以被接受,更新sta并进行递归即可,否则sum的值不变,判断下一个订单。

#include <stdio.h>#include <string.h>const int N = 30;const int INF = 0x3f3f3f;struct Order{int u,v,num;};Order o[N];int sta[N];int cap,n,m;int max;bool judge() {for(int i = 0; i < n; i++) {if(sta[i] > cap) {return false;}}return true;}void dfs(int cur,int sum) {if(max < sum) {max = sum;}if(cur >= m) {return ;}for(int i = o[cur].u; i < o[cur].v; i++) {sta[i] += o[cur].num;}if( judge()) {int tmp = o[cur].num * (o[cur].v - o[cur].u);dfs(cur+1,sum+tmp);}for(int i = o[cur].u; i < o[cur].v; i++) {sta[i] -= o[cur].num;}dfs(cur+1,sum);}int main() {while( scanf("%d%d%d",&cap,&n,&m) != EOF && (cap || n || m)) {memset(sta,0,sizeof(sta));for(int i = 0; i < m; i++) {scanf("%d%d%d",&o[i].u,&o[i].v,&o[i].num);}max = -INF;dfs(0,0);printf("%d\n",max);}return 0;}

0 0
原创粉丝点击