Transportation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 481 Accepted Submission(s): 180
Problem 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,经过 B 个城市,收到 t 个订单。
每个订单的信息分别是 起始城市 终点城市 和 人数。
要求对订单取舍使总收益最大。(订单不可分,即每个订单不可减少需经过的城市,不可只接运部分乘客,要么全留,要么全丢)。
此题数据量小(B<7 t<22),普通回溯可过,解子集树即可。
剪枝只剪了 人数大于客容量 的情况。
代码如下:
#include <iostream>
#define N 25
using namespace std;
int n,c,t; //客容量 城市 订单
int beg[N]; //起始城市
int tml[N]; //终点城市
int num[N]; //人数
int cit[N]; //记录各城市当前人数
int sum;
int ans;
void dfs(int x)
{
if(x==t)
{
ans=max(ans,sum);
return;
}
dfs(x+1);
int f=1;
for(int i=beg[x];i<tml[x];i++)
{
if(num[x]+cit[i]>n)
{
f=0;
break;
}
}
if(f)
{
sum+=num[x]*(tml[x]-beg[x]);
for(int i=beg[x];i<tml[x];i++) cit[i]+=num[x];
dfs(x+1);
for(int i=beg[x];i<tml[x];i++) cit[i]-=num[x];
sum-=num[x]*(tml[x]-beg[x]);
}
}
int main()
{
while(cin>>n>>c>>t&&(n||c||t))
{
for(int i=0;i<t;i++) cin>>beg[i]>>tml[i]>>num[i];
sum=0;
ans=0;
dfs(0);
cout<<ans<<endl;
}
return 0;
}