HDU 1074 Doing Homework

    xiaoxiao2022-06-30  77

    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 Hint In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.   Author Ignatius.L   Recommend

    We have carefully selected several similar problems for you:  1024 1078 1025 1080 1160 

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    状压DP~

    检查到吐血,string输入输出好像一定要用cin和cout,否则会变成笑脸……有兴趣的同学可以尝试一下……

    因为最多有15门课程,所以用一个二进制数来记录每一门课程是否取到,然后从1开始枚举每一位的状态,枚举到(1<<n)-1,<<优先级小一定要加括号。

    然后枚举每一位,看这门课程是否已选,如果已选,就用没选时的那个状态更新它,注意课程从n-1开始枚举到0,因为题中要求优先选先提出的课程。

    输出的时候用stack颠倒一下顺序。

    #include<cstdio> #include<cstring> #include<iostream> #include<stack> using namespace std; int t,n,maxx; stack<int> sta; struct node{ string name; int val,tim; }tot[16]; struct node1{ int val,num,la,tim; }ans[1<<15]; int main() { scanf("%d",&t); while(t--) { scanf("%d",&n); memset(ans,0,sizeof(ans)); for(int i=0;i<n;i++) cin>>tot[i].name>>tot[i].tim>>tot[i].val; maxx=(1<<n)-1; for(int i=1;i<=maxx;i++) { ans[i].val=999999999; for(int j=n-1;j>=0;j--) { int now=1<<j; if(i&now) { now=i-now; int kkz=ans[now].tim+tot[j].val-tot[j].tim; if(kkz<0) kkz=0; if(kkz+ans[now].val<ans[i].val) { ans[i].tim=ans[now].tim+tot[j].val; ans[i].num=j;ans[i].la=now; ans[i].val=ans[now].val+kkz; } } } } printf("%d\n",ans[maxx].val); while(maxx) { sta.push(ans[maxx].num);maxx=ans[maxx].la; } while(!sta.empty()) { cout<<tot[sta.top()].name<<endl;sta.pop(); } } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1125891.html

    最新回复(0)