hdu 1074 Doing Homework【状压dp】好题

来源:互联网 发布:苹果手机用4g网络很卡 编辑:程序博客网 时间:2024/06/08 09:49

Doing Homework

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

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.


题目大意: 

一共有N个作业,对应每个作业都有一个截止日期,对应要完成这个作业也需要对应的天数来完成,当一个作业如果在截止日期之后x天才提交,那么对应就要扣掉x分,问一个最小扣分分配的方案,如果有多种方案,对应输出字典序最小的那个。


思路:


1、首先观察到这个题的N并不大,只有15,那么考虑到状态压缩。

贪心可能会有疏漏点,考虑dp求解。


2、对应dp求解有两个问题:

①如何解决这个提交作业的顺序的问题,状态压缩只能压缩这个对应当前位的事物的有无,但是确定不了其顺序。

②如何解决这个扣除分数的问题。

我们首先来解决第一个问题:

①我们既然需要一个提交作业的顺序,那么我们对应将dp数组升一个维度,设定dp【i】【j】表示当前完成作业情况为j,并且最后一个做完的作业是第i个作业对应的最小花费。那么我们不难递推出其状态转移方程:dp【i】【j】=dp【k】【q】+扣除的分数值。这里k表示倒数第二个完成的作业。那么此时我们就有了一个顺序。

②然后我们解决这个扣除的分数的值的问题,我们对应解决这个问题设定一个数组sum【i】表示搞定了状态i的作业的时候,一共需要的用时。那么此时对应如果已经枚举出了这个四个值i,j,k,q,我们再设定一个变量tmp表示当前最后做这第i个作业需要扣除多少花费。那么这里tmp=第i个作业的截止日期-sum【j】;如果tmp现在的值大于0,那么设定tmp=0,相反,如果tmp的值小于0,那么对应tmp=-tmp;然后我们得到了此时状态转移需要扣除的分数值。


3、最终其解在dp【i】【(1<<n)-1】中,我们维护其中最小值,并且记录此时最后做的作业的编号为pos。

然后我们回溯记录答案即可。

注意这里需要字典序最小,因为我们是从最后一个作业向前找答案,那么当前答案的字典序越大越好,使得越靠前提交的作业的字典序对应也就越小。


4、注意数组的初始化、注意数组大小,其他的就没有什么了。


Ac代码:


#include<stdio.h>#include<string.h>#include<iostream>using namespace std;struct node{    char s[155];    int x,y;} a[155];int sum[(1<<17)];int dp[17][(1<<17)];int ans[17];int n;int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=0; i<n; i++)        {            scanf("%s%d%d",a[i].s,&a[i].x,&a[i].y);        }        memset(sum,0,sizeof(sum));        memset(dp,0x3f3f3f3f,sizeof(dp));        for(int i=0; i<(1<<n); i++)        {            for(int j=0; j<n; j++)            {                if((i&(1<<j))!=0)                {                    sum[i]+=a[j].y;                }            }        }        dp[0][0]=0;        for(int i=0; i<n; i++)        {            int tmp=a[i].x-a[i].y;            if(tmp>=0)tmp=0;            else tmp=-tmp;            dp[i][(1<<i)]=tmp;        }        for(int j=0; j<(1<<n); j++)        {            for(int i=n-1; i>=0; i--)            {                if((j&(1<<i))!=0)                {                    for(int k=n-1; k>=0; k--)                    {                        if((j&(1<<k))!=0)                        {                            int q=j-(1<<i);                            int tmp=a[i].x-sum[j];                            if(tmp>=0)tmp=0;                            else tmp=-tmp;                            dp[i][j]=min(dp[i][j],dp[k][q]+tmp);                        }                    }                }            }        }        int output=0x3f3f3f3f;        int pos;        for(int i=n-1; i>=0; i--)        {            if(dp[i][(1<<n)-1]<output)            {                output=dp[i][(1<<n)-1];                pos=i;            }        }        int cont=0;        int tmp=(1<<n)-1;        while(1)        {            int out;            int minn=0x3f3f3f3f;            for(int i=n-1;i>=0;i--)            {                if(dp[i][tmp]<minn)                {                    minn=dp[i][tmp];                    out=i;                }            }            ans[cont++]=out;            if(cont==n)break;            tmp-=(1<<out);        }        printf("%d\n",output);        for(int i=cont-1; i>=0; i--)        {            printf("%s\n",a[ans[i]].s);        }    }}



0 0