Transportation
Transportation |
Ruratania is just entering capitalism and is establishing new enterprisingactivities in many fields including transport.The transportation company TransRuratania is starting a new express trainfrom city Ato city B with several stops in the stations on the way. The stationsare successively numbered, city A stationhas number 0, city B station number m. The company runs an experimentin order to improve passengertransportation capacity and thus to increase its earnings. The train hasa maximum capacity n passengers.The price of the train ticket is equal to the number of stops (stations)between the starting station and thedestination station (including the destination station). Before thetrain starts its route from the city A, ticketorders are collected from all onroute stations. The ticket order from thestation S means all reservations oftickets from S to a fixed destination station. In case the company cannotaccept all orders because of thepassenger capacity limitations, its rejection policy is that it eithercompletely accept or completely reject single orders from single stations.
Write a program which for the given list of orders from single stationson the way from A to B determinesthe biggest possible total earning of the TransRuratania company. Theearning from one accepted order isthe product of the number of passengers included in the order and theprice of their train tickets. The totalearning is the sum of the earnings from all accepted orders.
Input
The input file is divided into blocks. The first line in each block containsthree integers: passengercapacity n of the train, the number of the city B station and the numberof ticket orders from all stations.The next lines contain the ticket orders. Each ticket order consists ofthree integers: starting station,destination station, number of passengers. In one block there can be maximum22 orders. The number ofthe city B station will be at most 7. The block where all three numbers inthe 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 inputfile except the terminatingblock. 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
解决方案:关于每个订单,有签与不签两种情况,可根据这两种情况进行回溯解决。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node{
int st,en;
int passenger;
}O[30];
int sum[30];
int limit,B,order;
int Max;
void dfs(int s){
if(s==order){
int temp=0;
for(int i=0;i<B;i++){
temp+=sum[i];
}
if(temp>Max) Max=temp;
return ;
}
// cout<<"s";
bool flag=true;
for(int i=O[s].st;i<O[s].en;i++){
if(sum[i]+O[s].passenger>limit)
flag=false;///车满载,订单不合要求。
}
if(flag){
for(int i=O[s].st;i<O[s].en;i++){
sum[i]+=O[s].passenger;
}
dfs(s+1);
for(int i=O[s].st;i<O[s].en;i++)
sum[i]-=O[s].passenger;
}
dfs(s+1);
}
int main(){
while(~scanf("%d%d%d",&limit,&B,&order)&&(limit+B+order)){
for(int i=0;i<order;i++){
scanf("%d%d%d",&O[i].st,&O[i].en,&O[i].passenger);
}
Max=0;
memset(sum,0,sizeof(sum));
dfs(0);
cout<<Max<<endl;
}
return 0;
}