In every summer there are students graduating from school. Alot of graduating studentswould like to hosttheir parties to say good-bye to their classmates and friends. This kind of activity iscalled "bg"on our BBS.
Attendingdifferent parties will give you different feelings. You may assign each party a degree ofhappiness which is a non-negative integer that shows how you feel about attending to thatparty.
Nowyou are given a list of parties containing the length of each party and the time on which the host willbe leaving school. It would be nice to schedule the parties so that you can obtainmaximum of happiness. Since there could be as many as 30 parties on the list, you'llneed the computer to help you.
InputSpecification:
Your program must read test casesfrom a file “input.txt”. The inputfile consists of several testcases. Each case starts with an integer N(≤30),then followed by N lines each in the format h l t .whereh is the degree of happiness, l is the length of the party (in hours), and t is the hoursafter which the host will be leaving the school. It is guaranteed that l is no greaterthan t, since if the host will leave at the end of the t-th hour, the party will haveto end by that time.
The input is finished by a negative N.
OutputSpecification:
For each test case, you are supposedto print to a file “output.txt”.First print in one line the maximumdegree of happiness. In the next line print a possible schedulein the format: i1 i2 ……ik, where i1 i2 ……ik are theindex numbers of the parties (start counting from 1).
Note: once you select a party, youmust join the party from the beginning to the end. The time taken to run from one party to another can beomitted.
In case that the solutions are notunique, output any one of them will do.
SampleInput:
4
5 1 1
10 2 3
6 1 2
3 1 1
-1
SampleOutput:
16
3 2
#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXSIZE=30;
//用于保存路径的栈类
class Stack{
public:
Stack(int size);
void Copy(Stack* stack);
void Push(int x);
int Pop();
int Where(int i);//返回第i位置的元素
int Size();
bool Empty();
bool Full();
private:
int top;
int* stackArray;
int length;
};
//函数//////////////////////////////////
Stack::Stack(int size) : length(size) {
top=-1;
stackArray=new int[length];
}
void Stack::Copy(Stack* stack){
while(!this->Empty())
this->Pop();
for(int i=0;i<stack->Size();++i)
this->stackArray[++top]=stack->Where(i);
}
void Stack::Push(int x){
if(!Full())
stackArray[++top]=x;
}
int Stack::Pop(){
if(!Empty())
return stackArray[top--];
else return -1;
}
int Stack::Size(){
return top+1;
}
bool Stack::Empty(){
return -1==top ? true : false ;
}
bool Stack::Full(){
return length-1==top ? true : false ;
}
int Stack::Where(int i){
return stackArray[i];
}
////////////////////////////////
//
//用于存储的数据结构
struct Party{
int partyID; //party的编号
int happiness;
int partyLastTime;
int hostTime;
Party* next;
};
class BiggestHappiness{
public:
BiggestHappiness(char* input , char* output);
void FindParties(Party* partyNow, int time, int sumHappiness); //partyNow表示当前应处理的party,time表示处理当前party之时前面一共用掉的时间
void Operate();
private:
Party* head;
int biggestHappiness;
ifstream* inFile;
ofstream* outFile;
Stack* maxHapStack; //用于记录当前最大happiness的party的栈
Stack* hapStack; //用于记录当前正在计算的路径的party栈
};
BiggestHappiness::BiggestHappiness(char* input, char* output){
inFile=new ifstream(input,ios::in);
outFile=new ofstream(output,ios::out);
head=new Party;
head->next=NULL;
maxHapStack=new Stack(MAXSIZE);
hapStack=new Stack(MAXSIZE);
}
void BiggestHappiness::FindParties(Party* partyNow, int time, int sumHappiness){
if(NULL==partyNow){
if(biggestHappiness<sumHappiness){
maxHapStack->Copy(hapStack);
biggestHappiness=sumHappiness;
}//当前得到的结果比之前最大的还要大
return ;
}
if( time <= partyNow->hostTime - partyNow->partyLastTime ){//表明当前party可以参加
//不参加此party
FindParties(partyNow->next,time,sumHappiness);
//参加此party
hapStack->Push(partyNow->partyID);
FindParties(partyNow->next, time+partyNow->partyLastTime, sumHappiness+partyNow->happiness);
hapStack->Pop();
}
else FindParties( partyNow->next , time , sumHappiness );//当前party不可参加
}
void BiggestHappiness::Operate(){
while(*inFile>>head->partyID , head->partyID > 0 ){
Party* p=head;
Party* s;
head->next=NULL;
biggestHappiness=0;
for(int i=0; i<head->partyID; ++i){
s=new Party;
s->partyID=i+1;
s->next=NULL;
*inFile>>s->happiness>>s->partyLastTime>>s->hostTime;
/*下面是插入排序的过程*/
p=head;
while(p->next!=NULL){
if(p->next->hostTime<s->hostTime) p=p->next;
else{
s->next=p->next;
p->next=s;
break;
}
}
if(NULL==p->next)
p->next=s;
}
FindParties(head->next, 0, 0);
*outFile<<biggestHappiness<<endl;
for(int i=0;i<maxHapStack->Size();++i)
*outFile<<maxHapStack->Where(i)<<" ";
*outFile<<endl;
/*下面释放内存*/
for(p=head->next , s=p->next ; s!=NULL; ){
delete p;
p=s;
s=s->next;
}
delete p;
/****************/
}
}
///主函数
int main(){
BiggestHappiness happyParty("input.txt","output.txt");
happyParty.Operate();
return 0;
}