Doing Homework (状压dp)

来源:互联网 发布:为什么离婚 知乎 编辑:程序博客网 时间:2024/05/19 17:48

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.




一开始想的暴力的话基本上是15的阶乘不现实。

一开始倒是想过枚举每一道题目,然后dp[i]表示做完i个题时的最少费用,然后发现状态很乱,有重复,没法控制。

其实 n = 15 就应该直接想到状压来解决,不得不说状压的确是个神奇的东西,可以把枚举15阶乘的复杂度给降下来。不然也不能叫dp了是吧。

状压,用二进制表示哪几道题目已经处理好了。比如说5就是101表示第1道题目和第三道题目已经处理完成了。

然后就可以转移了。dp[state]表示在当前的状态下可以获得的最小代价。

那么,枚举state里面1的位置上的题目,依次更新答案就可以了(具体看代码吧)。

#include <bits/stdc++.h>using namespace std;const int MAXN = 1e6+7;const int inf = 1e9;int n,m;struct node{    char name[105];    int dead,need;} p[20];struct dp{    int score,tim,pre,now;} dp[1<<15];int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(dp,0,sizeof dp);        scanf("%d",&n);        for(int i = 0 ; i < n ; ++i)scanf("%s%d%d",p[i].name,&p[i].dead,&p[i].need);        int e = 1 << n;        for(int s = 1 ; s < e ; ++s)        {            dp[s].score = inf;            for(int i  = 0 ; i < n ; ++i)//因为是按照字典序从小到大来枚举的            {                int temp = 1 << i;                if(s & temp)                {                    int ago = s - temp;                    int tim = dp[ago].tim + p[i].need - p[i].dead;                    tim = max(tim,0);                    if(dp[ago].score + tim <= dp[s].score)//所以这里应该是<=表示代价相同的时候,更新字典序大的,剩下的自然是字典序小的                    {                        dp[s].score = dp[ago].score + tim;                        dp[s].tim = dp[ago].tim + p[i].need;                        dp[s].pre = ago;                        dp[s].now = i;                    }                }            }        }        printf("%d\n",dp[e-1].score);        stack<int>S;        int v = e-1;        while(v)        {            S.push(dp[v].now);            v = dp[v].pre;        }        while(!S.empty())        {            printf("%s\n",p[S.top()].name);            S.pop();        }    }    return 0;}







原创粉丝点击