题目描述:
Transportation
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
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
题意:上车 容量考虑。
题解:DFS,注意 DFS的对象 不同 不然容易TLE。
代码:
/*
uva 301 Transportation
Brute Force set
we can make a tag array to the station capacity instead of search a whole Orders array to find the people num > train capacity
the DFS's structrue is fail,it is so wasting time
we should clearly define the the recursive object , it must be that we would how to choose the object as the solution,so this object is our recursive main object
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n=0;//the capacity of the train
int s=0;//the number of the stations 0~s
int t=0;//the number of the ticket orders
/*the struction of the ticket order*/
typedef struct TicOrder
{
int Start;
int End;
int Num;
bool Selected;
TicOrder()
{
Start=0;
End=0;
Num=0;
Selected=false;
}
}TicOrder,*TicOrderLink;
TicOrder TicOrders[22+5];//the queue of the ticket orders
int StaCapa[8]={0};//station capacity of the train in station i
int MaxEarning=0;//the biggest possible earning
int Earning=0;
/*compare for the sort*/
bool cmp(const TicOrder &A,const TicOrder &B)
{
if(A.Start<B.Start)
{
return(true);
}
else if(A.Start>B.Start)
{
return(false);
}
else
{
if(A.End<B.End)
{
return(true);
}
else
{
return(false);
}
}
}
/*the cur is the number of the stations,we think maybe it did not need recursive structure*/
int DFS(int people,int cur)
{
if(cur>=s)
{
if(Earning>MaxEarning)
{
MaxEarning=Earning;
}
return(0);
}
else
{
int i=0;
int left=n-people;
int GetOn=0;//the number of people get on the train
int GetOff=0;//the number of people get off the train
//first get off second get on
//do we have to enume from start to end ???
for(i=0;i<=t-1;i++)
{
if(TicOrders[i].End==cur&&TicOrders[i].Selected==true)
{
GetOff+=TicOrders[i].Num;
TicOrders[i].Selected=false;
left+=TicOrders[i].Num;//note : not sub the getoff
people-=TicOrders[i].Num;
}
if(TicOrders[i].Start==cur)
{
if(left>=TicOrders[i].Num&&TicOrders[i].Selected==false)//we should add the contrary strategy in the same condition
{
TicOrders[i].Selected=true;//it is selected to the train
Earning+=TicOrders[i].Num*(TicOrders[i].End-TicOrders[i].Start);
GetOn=TicOrders[i].Num;
DFS(people+GetOn,cur);//maybe the multiple orders in same start stations
//there is a structure problem in the recursive because of the same cur recursive
TicOrders[i].Selected=false;
Earning-=TicOrders[i].Num*(TicOrders[i].End-TicOrders[i].Start);
}
}
}
DFS(people,cur+1);//the train have no access to accept passengers same stations
}
return(0);
}
/*new dfs,the cur is the oders index*/
int NewDFS(int cur)
{
if(cur>=t)
{
if(Earning>MaxEarning)
{
MaxEarning=Earning;
}
return(0);
}
else
{
int i=0;
int flag=0;//not more than the train limit
int Temp[8]={0};//save the StaCapa,because of the recursive structure
memcpy(Temp,StaCapa,sizeof(StaCapa));
for(i=TicOrders[cur].Start;i<=TicOrders[cur].End-1;i++)//it not include the rear end because the people had got off
{
StaCapa[i]+=TicOrders[cur].Num;
if(StaCapa[i]>n)
{
flag=1;
break;
}
}
if(!flag)//we can take the people on the train
{
Earning+=TicOrders[cur].Num*(TicOrders[cur].End-TicOrders[cur].Start);
NewDFS(cur+1);
Earning-=TicOrders[cur].Num*(TicOrders[cur].End-TicOrders[cur].Start);
}
memcpy(StaCapa,Temp,sizeof(Temp));
NewDFS(cur+1);
}
return(0);
}
/*for test*/
int test()
{
return(0);
}
/*main process*/
int MainProc()
{
while(scanf("%d%d%d",&n,&s,&t)!=EOF&&(n!=0||s!=0||t!=0))
{
int i=0;
MaxEarning=0;
Earning=0;
memset(StaCapa,0,sizeof(StaCapa));
for(i=0;i<=t-1;i++)
{
scanf("%d%d%d",&TicOrders[i].Start,&TicOrders[i].End,&TicOrders[i].Num);
TicOrders[i].Selected=false;
}
sort(TicOrders,TicOrders+t,cmp);
//sequence of the recursive scan
//int people=0;//the now people's number in the train
//DFS(people,0);//the 0 is the number of the stations
NewDFS(0);
printf("%d\n",MaxEarning);
}
return(0);
}
int main()
{
MainProc();
return(0);
}