#include<cstdio>#include<cstring>using namespace std;int n,m,k,seat[30],accept[30],max;typedef struct ord{ int start,end,number,earn;} ord;ord order[30];void dfs(int stop,int money,int cur){ max=max>money?max:money; if(stop==m) return ; int i,j; for(i=cur; i<k; i++) { if(order[i].start==stop&&!accept[i]) { int flag=1; for(j=stop; j<order[i].end; j++) if(seat[j]<order[i].number) { flag=0; break; } if(flag) { for(j=stop; j<order[i].end; j++) seat[j]-=order[i].number; accept[i]=1; dfs(stop,money+order[i].earn,i+1); accept[i]=0; for(j=stop; j<order[i].end; j++) seat[j]+=order[i].number; } } else if(order[i].start>stop) break; } dfs(stop+1,money,cur);}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d%d%d",&n,&m,&k)!=EOF&&(n||m||k)) { for(int i=0; i<=m; i++) seat[i]=n; for(int i=0; i<k; i++) { scanf("%d%d%d",&order[i].start,&order[i].end,&order[i].number); order[i].earn=(order[i].end-order[i].start)*order[i].number; accept[i]=0; } for(int i=0; i<k; i++) { for(int j=i+1; j<k; j++) { if(order[i].start>order[j].start) { ord temp; temp=order[i]; order[i]=order[j]; order[j]=temp; } } } max=0; dfs(0,0,0); printf("%d\n",max); } return 0;}
The Code imitates what others write. It mainly involves backtracking. at each stop station, you can choose to let passengers get on the bus or choose not. The global variable seat marks the number of people on the bus at all times, used to determine whether a passenger can get on the bus. In DFS, stop is the current station and cur is the current command. Write this for the time being and try again!