题目要求
Description
Ruratania is just entering capitalism and is establishing new enterprising activities in many fields in- cluding 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
10 3 4
0 2 1
1 3 5
1 2 7
2 3 10
10 5 4
3 5 10
2 4 9
0 2 5
2 5 8
0 0 0
Sample Output
19
34
算法思路
基本思路就是对给定的n种票型做深度优先搜索。用一个全局数组表示7段路径上剩余的容量。dfs函数传递两个参数,一个代表此时的收入,一个代表遍历到的票型的序号。
有两点需要注意:一是对输入的票型根据起始站升序排列,这样在判断火车容量是否足够时,只需要判断起始站火车当前容量是否大于上车人数,因为当前之后的路程上火车容量一定大于等于起始站;二是调用dfs之后要将火车容量复原,这样才能遍历到所有情况。
剪枝操作就是对tickets增加两个属性,其中rest是包含此票型在内的剩余所有票型的总收入,在搜索时,如果当前收入+rest<=目前的收入最大值,则可以剪去。
参考:https://www.cnblogs.com/dengeven/p/3241492.html
程序代码
#include<iostream>
#include<algorithm>
using namespace std;
struct tickets
{
int start, end, num;
int value, rest;//用于剪枝优化
}ticket[22];
int cap, B_num, ord_num, earn;
int train_cap[7];//7段路径上的剩余容量
void dfs(int money, int seq)
{
if (money > earn)
earn = money;
for (int i = seq; i < ord_num; i++)
{
if (money + ticket[i].rest <= earn)//剪枝
return;
if (train_cap[ticket[i].start] >= ticket[i].num)
{
for (int j = ticket[i].start; j < ticket[i].end; j++)
{
train_cap[j] -= ticket[i].num;
}
dfs(money + ticket[i].value, i + 1);
for (int j = ticket[i].start; j < ticket[i].end; j++)
{
train_cap[j] += ticket[i].num;
}
}
}
}
int main()
{
while (true)
{
cin >> cap >> B_num >> ord_num;
earn = 0;
if (cap == 0 && B_num == 0 && ord_num == 0)
break;
for (int i = 0; i < ord_num; i++)
{
cin >> ticket[i].start >> ticket[i].end >> ticket[i].num;
for (int j = i; j > 0; j--)//按照start排序
{
if (ticket[j].start < ticket[j - 1].start)
{
int a = ticket[j].start;
ticket[j].start = ticket[j - 1].start;
ticket[j - 1].start = a;
a = ticket[j].end;
ticket[j].end = ticket[j - 1].end;
ticket[j - 1].end = a;
a = ticket[j].num;
ticket[j].num = ticket[j - 1].num;
ticket[j - 1].num = a;
}
else
break;
}
}
for (int i = 0; i < ord_num; i++)
{
ticket[i].value = (ticket[i].end - ticket[i].start) * ticket[i].num;
}
int sum = 0;
for (int i = ord_num - 1; i >= 0; i--)
{
sum += ticket[i].value;
ticket[i].rest = sum;
}
for (int i = 0; i < B_num; i++)
train_cap[i] = cap;
dfs(0, 0);
cout << earn << endl;
}
return 0;
}