hdu1074Doing Homework( 状态压缩dp)

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门课程作业,每门作业的截止时间为D,需要花费的时间为C,若作业不能按时完成,每超期1天扣1分。
这n门作业按课程的字典序先后输入
问完成这n门作业至少要扣多少分,并输出扣分最少的做作业顺序
PS:达到扣分最少的方案有多种
,请输出字典序最小的那一组方案

状态:dp[i]指的是i(i二进制分解后为1的是完成的任务)状态下能减掉最小的分数,这里在这一个状态里面有许多顺序,都压缩在这一个状态里面了

转移方程:dp[i]=min(dp[i],dp[i-temp]+score),这里的score指的是在i-temp(也就是i状态下当前任务未完成所在的状态,有点像背包递推方程形式)状态下,再完成当前任务所额外需要扣除的分数。

/*分析:对于n种家庭作业,全部做完有n!种做的顺序
但是n!太大了,且对于完成作业1,2,3和1,3,2和2,1,3和3,2,1和3,1,2来说
完成它们消耗的天数一定是一样的,只是完成的顺序不同从而扣的分不同
所以可以将完成相同的作业的所有状态压缩成一种状态并记录扣的最少分即可
即:状态压缩dp
对于到达状态i,从何种状态到达i呢?只需要枚举所有的作业
假如对于作业k,i中含有作业k已完成,那么i可以由和i状态相同的状态仅仅是k未完成的
状态j=i-(1<<k)来完成k到达,并且j一定比i小,如果状态从0枚举到2^n-1那么j一定是在i之前已经计算过的
*/
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MAX ( (1<<15)+10 )
typedef long long LL;
using namespace std;

///课程信息 可以封装成结构体
int deadT[20],cost[20];
char s[20][110];

///DP的中间状态信息,可以封装成结构体
int dp[MAX],tim[MAX],pre[MAX];///dp[i]记录到达状态i扣的最少分,t是在dp[i](扣除最小分)相应的花去多少天了

void output(int x){
	if(!x)return;
	output(x-(1<<pre[x]));
	printf("%s
",s[pre[x]]);
}

int main(){
	int T,n;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=0;i<n;++i)scanf("%s%d%d",&s[i],&deadT[i],&cost[i]);
		int bit=1<<n ;
		for(int i=1;i<bit;++i)///枚举到达状态i
        {
			dp[i]=INF;///初始化到达状态i的扣分
			for(int j=n-1;j>=0;--j){///由于输入时按字符大小输入,而每次完成j相当于把j放在后面完成
                                     ///这里下面判断是dp[i]>dp[i-temp]+score,所以是n-1开始
                                     ///是不是感觉我这句话说反了,这里应该注意的是在遍历的j个作业是最后一个完成的
                                     ///所以这里应该是下面if(所有的1)里面能完成的最大的,而不是最小的
                                     ///如果下面判断是dp[i]>=dp[i-temp]+score则从0开始

				int temp=1<<j;
				if(!(i&temp))  continue;///状态i不存在作业j完成则不能通过完成作业j到达状态i
				int score=tim[i-temp]+cost[j]-deadT[j];///i-temp表示没有完成j的那个状态  score是在当前情形下完成j所扣除的分数
				if(score<0)score=0;///完成j被扣分数最小是0
				if(dp[i]>dp[i-temp]+score){
					dp[i]=dp[i-temp]+score;
					tim[i]=tim[i-temp]+cost[j];///到达状态i花费的时间
					pre[i]=j;///到达状态i的前驱,为了最后输出完成作业的顺序
				}
			}
		}
		printf("%d
",dp[bit-1]);
		output(bit-1);///输出完成作业的顺序
	}
	return 0;
}


分析:
n<=15,由题意知,只需对这n份作业进行全排列,选出扣分最少的即可。
用一个二进制数存储这n份作业的完成情况,第1.。。。n个作业状况分别
对应二进制数的第0,1.。。。。,n-1位则由题意,故数字上限为2^n
其中 2^n-1即为n项作业全部完成,0为没有作业完成。。。

用dp[i]记录完成作业状态为i时的信息(所需时间,前一个状态,最少损失的分数)。
递推条件如下
1.状态a能做第i号作业的条件是a中作业i尚未完成,即a&i=0。
2.若有两个状态dp[a],dp[b]都能到达dp[i],那么选择能使到达i扣分小的那一条路径,若分数相同,转入3
3.这两种状态扣的分数相同,那么选择字典序小的,由于作业按字典序输入,故即dp[i].pre = min(a,b);

初始化:dp[0].cost = 0;dp[0].pre=-1;dp[0].score= 0;

最后dp[2^n-1].score即为最少扣分,课程安排可递归的输出


状态(和上面一种方法一样):dp[i]指的是i(i二进制分解后为1的是完成的任务)状态下能减掉最小的分数,这里在这一个状态里面有许多顺序,都压缩在这一个状态里面了

递推方程: dp[ newstate ].score = min(dp[oldstate].score+score, dp[oldstate].score)    这里的oldstate就是i状态下,newstate就是在i状态下在完成clsID任务所在i 的 新状态(i | 1<<clsID)
这个递推公式与上面的区别在于上面的是dp[i]在遍历过程中当前的任务还没做,有点像背包递推形式,更新的是dp[i]
                                                    而这一个dp[i] (i是旧状态) 遍历的时候是在当前i基础上做当前任务,更新的dp[新状态]

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MAXN (1<<16)
struct Node
{
    int cost;   ///所需时间
    int pre;    ///前一状态
    int score;///最少损失的分数
}dp[MAXN];


struct Course
{
    int deadtime;  ///截止日期
    int cost;      ///所需日期
    char name[201];
}course[16];


void output(int status)  ///因为记录的是状态,所以输出有些麻烦
{
    int curjob=dp[status].pre^status;
    int curid=0;
    curjob>>=1;
    while(curjob)
        curid++,curjob>>=1;
    if(dp[status].pre!=0)
        output(dp[status].pre);
    printf("%s
",course[curid].name);
}
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s%d%d",&course[i].name,&course[i].deadtime,&course[i].cost);


        memset(visited,false,sizeof(visited));
        for(int i=0;i<(1<<n);i++)
           dp[i].score=INF;


        dp[0].cost=0;
        dp[0].pre=-1;
        dp[0].score=0;///dp[0]是指所有作业都没有做的状态


        int tupper=(1<<n)-1;///tupper表示成二进制数是n个1的,表示所有的作业都完成了


        for(int i=0;i<tupper;i++)///遍历所有状态
        {
            for(int clsID=0;clsID<n;clsID++)
            {
                int cur=1<<clsID;
                if((cur&i)==0)///该项作业尚未做过
                {
                    int curtemp= cur | i; ///在当前i状态下再做作业clsID后进入的状态


                    dp[curtemp].cost=dp[i].cost+course[clsID].cost; ///新状态完成需要的时间,这个和顺序是没有关系的


                    int score=dp[curtemp].cost-course[clsID].deadtime;///在i状态下再完成当前作业额外的分数
                    if(score<0)  score=0;


                    if(dp[i].score+score<dp[curtemp].score)
                    {
                        dp[curtemp].score = dp[i].score+score;
                        dp[curtemp].pre=i;
                    }
                }
            }
        }
        printf("%d
",dp[tupper].score);
        output(tupper);///递归输出
    }
}

第一次正儿八经的做状压dp,整理这样还算凑合吧!!!





原文地址:https://www.cnblogs.com/zswbky/p/6792921.html