Doing Homework(数塔型状态DP)

来源:互联网 发布:网络理财投资推荐 编辑:程序博客网 时间:2024/06/06 01:55

Problem  Link :http://acm.hdu.edu.cn/showproblem.php?pid=1074

Original  Problem:


Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5234    Accepted Submission(s): 2187


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
23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3
 

Sample Output
2ComputerMathEnglish3ComputerEnglishMath
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
 

编程思想:数塔型状态DP。

题目分析:

图示说明(假设有3门功课):






可以自底向上的计算!

本题数据范围较小,但暴力枚举的话肯定超时,因为最坏情况将要枚举15!种情况,因此只有采取dp。

本题有点类似于经典的数塔模型,此处用已完成的课程表示当前的状态,对于某门课程,0表示还未完成,1表示已完成,采用按位压缩的方式表示出所有的情况,因此最多有1<<15种状态。还有一点要注意,就是题目中所说的选择字典序最小的输出。由于原文输入是按字典序输入的,见“Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.”,因此对应相应的dp顺序即可,无须排序操作。具体的请看程序的具体实现。



----------------------------------------------------------------------------code---------------------------------------------------------------------------------------------------
//状态压缩dp,类似于数塔模型。  
//对应位上,1表示已完成该课程,0表示未完成   
//如001表示课程1已完成,而2,3未完成   

#include<stdio.h>
const int INF=100000000;
struct node{
char s[111];
int d;
int c;
}work[16];
struct node1{
int now;
int time;
int score;
}dp[1<<15];//now为当前加入的课程编号,time为累加的天数,score为累加处罚分   
void path(int i)//输出路径   
{
if(i==0)
return;
path(i-(1<<dp[i].now));
printf("%s\n",work[dp[i].now].s);
}
int main()
{
int t,n,m,i,j,k,temp;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s%d%d",&work[i].s,&work[i].d,&work[i].c);
dp[0].now=0;//初始化 
dp[0].score=0;
dp[0].time=0;
m=(1<<n)-1;
for(i=1;i<=m;i++)
dp[i].score=INF;
for(i=0;i<=m;i++)//类似于数塔,最底层表示一门作业也没完成(0),然后上一层表示已完成一门课程 
{                            //依次递推,直到完成了所有的课程,即到达顶层(m)
for(j=0;j<n;j++)//课程  
{
if((i&(1<<j))==0)//若第j门课程还未完成,注意:位运算符要加括号!!! 
{
k=(i|(1<<j));
temp=dp[i].time+work[j].c-work[j].d;
if(temp<0)
temp=0;
if(dp[k].score>dp[i].score+temp)
{
dp[k].score=dp[i].score+temp;
dp[k].now=j;
dp[k].time=dp[i].time+work[j].c;
}
}
}
}
printf("%d\n",dp[m].score);
path(m);
}
}
return 0;
}


0 0