首页 > 代码库 > HDU-1074 Doing Homework
HDU-1074 Doing Homework
http://acm.hdu.edu.cn/showproblem.php?pid=1074
递归求输出。dp[i]是一个结构体,
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5152 Accepted Submission(s): 2131
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.
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. 分析:dp[i].pre结构体记录上一行的状态。
枚举上一行状态到i城市的最小发费。
#include<iostream>#include<cstring>#include<cstdio>#define inf 1<<27using namespace std;int n,mark[32868],c[40];struct node{ char str[200]; int x,y;}a[40];struct no{ int cost;//做的天数 int pre;//上一行的状态。 int reduce;//处罚}dp[32868];void output(int s)//递归输出。{ int i=s^dp[s].pre; int j=0; while(i) { i>>=1; j++; }//判断是哪个城市 if(dp[s].pre!=0) { output(dp[s].pre); } printf("%s\n",a[j].str);}int main(){ int t,s,j,k,q,i; scanf("%d",&t); while(t--) { memset(mark,0,sizeof(mark)); scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%s%d%d",a[i].str,&a[i].x,&a[i].y); } dp[0].pre=-1; dp[0].cost=0; dp[0].reduce=0; mark[0]=1; for(s=0;s<1<<n;s++)//上一行状态。 { for(i=1;i<=n;i++)//到这个城市。 { if((s&1<<i-1)!=0) continue; int r=s|(1<<i-1);//到这个城市之后的状态。 int day=dp[s].cost+a[i].y;//发费的总天数。 dp[r].cost=day; int reduce=day-a[i].x;//处罚数 if(reduce<0)reduce=0; reduce+=dp[s].reduce; if(mark[r])//标记这个状态有没有。 { if(dp[r].reduce>reduce) { dp[r].reduce=reduce; dp[r].pre=s; } } else//状态没有 { mark[r]=1; dp[r].reduce=reduce; dp[r].pre=s; } } } printf("%d\n",dp[(1<<n)-1].reduce); output((1<<n)-1); } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。