Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject's homework). Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier. Output For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one. Sample Input 2 3 Computer 3 3 English 20 1 Math 3 2 3 Computer 3 3 English 6 3 Math 6 3 Sample Output 2 Computer Math English 3 Computer English Math 说是什么DP,我实在想不出来。baidu一下。原来是深搜加DP。用到了位表示。算是一个比较典型的搜索了,个人觉得DP思想在这里不是很重#include<stdio.h>#include<string.h>int work[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};//表示该作业完成。二进制表示。1表示第一个作业完成。2表示第二个,4表示第三个。都为2的倍数int flag[65538];//表示有那么多作业完成时的最小花费int mark[16];//记录是否已完成int N;struct node { char name[1000]; int cost; int endtime;}NODE[16];char out[16][1000], str[16][1000];void dfs(int start,int cost,int num,int works)//start 作业开始时间。cost前面已花费的时间。num已完成数目,works 作业完成情况{ int i,temp; if(N==num)//所有都已完成 { if(cost==flag[works]) { flag[works]=cost; for(i=0;i<num;i++) strcpy(out[i],str[i]); } return ; } for(i=1;i<=N;i++) { if(mark[i]==0) { mark[i]=1; works+=work[i]; temp=start+NODE[i].cost-NODE[i].endtime; if(temp<0) temp=cost; else temp+=cost; if(flag[works]>temp) { flag[works]=temp; strcpy(str[num],NODE[i].name); num++; dfs(start+NODE[i].cost,temp,num,works); num--; } works-=work[i]; mark[i]=0; } } }int main(){ int n,i,j,sum; scanf("%d",&n); while(n--) { scanf("%d",&N); for(i=1;i<=N;i++) { scanf("%s%d%d",&NODE[i].name,&NODE[i].endtime,&NODE[i].cost); } for(i=1;i<=15;i++) mark[i]=0; for(i=0;i<=65537;i++) flag[i]=10000000; dfs(0,0,0,0); sum=0; for(i=1;i<=N;i++) sum+=work[i]; printf("%d\n",flag[sum]); for(i=0;i<N;i++) { for(j=0;out[i][j]!='\0';j++) printf("%c",out[i][j]); printf("\n"); } } return 0;}

评论