HDU 1074 Doing Homework (状态压缩DP)

Doing Homework

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

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.

Author

Ignatius.L

#include<iostream>
#include<string.h>
#include<stdio.h>
#define N (1<<15)+5
#define M 120
#define INF 0x3f3f3f3f
using namespace std;
int dp[N];//dp[i]表示状态i时最少扣的分数
struct node
{
	char name[M];
	int d,c;//截止日期,花费时间
}a[20];
int path[N];//path[i]=k表示i状态是由k状态转移过来的
int t,n;
/*
打印路径就是在状态转移的时候用一个数组记录下来,一个状态是由哪一个状态成功转移过来的,然后最后打印路径的时候就是反着这个根据用递归输出出来就行了
*/
void print(int x)
{
	if(x==0)
		return;
	int t=0;
	for(int i=0;i<n;i++)
	{
		if( (x&(1<<i)) !=0 && (path[x]&(1<<i)) ==0 )//有这个状态有交际,并且能补全上一个状态
		{
			t=i;
			break;
		}
	}
	print(path[x]);
	printf("%s\n",a[t].name);
}
/*
状态的转移:先枚举二进制的状态,每枚举到一个状态的时候,在这个状态中遍历所有课程,若这门课程完成了那么已经包括在这个状态中了,如果没完成就要
像01背包那样讨论完成和不完成的罚时到底哪个小,假如现在不完成的罚时小,那么这门课就先不完成,后面在完成。
*/
int main()
{
	//freopen("in.txt", "r", stdin);
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d\n",&n);
		for(int i=0;i<n;i++)
			scanf("%s %d %d\n",&a[i].name,&a[i].d,&a[i].c);
		memset(dp,INF,sizeof dp);
		memset(path,0,sizeof path);
		dp[0]=0;//什么课也没开始上罚时当然是0了
		int tol=(1<<n);
		for(int i=0;i<tol;i++)//枚举状态
		{
			for(int j=n-1;j>=0;j--)//枚举那门课
			{
				if((i&(1<<j))==0)//这门课上没上,找出每一个当前功课没完成的状态,然后由这个状态转移到完成了当前课程的状态
				{
					int s=0;
					for(int k=0;k<n;k++)//计算这个状态到现在花费的时间
						if(i&(1<<k))//这门课上了
						s+=a[k].c;
					s+=a[j].c;//再加上j这门课花费的时间
					int time=s-a[j].d;//计算罚时
					if(time<0)
						time=0;
					if(dp[i|(1<<j)]>dp[i]+time)
					{
						dp[i|(1<<j)]=dp[i]+time;
						path[i|(1<<j)]=i;//如果这个状态转移过来了,就记录下来是由哪个状态转移过来的
					}
					//cout<<dp[i|(1<<j)]<<endl;
				}
			}
		}
		//for(int i=0;i<tol;i++)
		//	cout<<path[i]<<" ";
		//cout<<endl;
		printf("%d\n",dp[tol-1]);
		print(tol-1);
	}
	return 0;
}

  

时间: 2025-01-16 19:25:59

HDU 1074 Doing Homework (状态压缩DP)的相关文章

HDU 1074 Doing Homework(状态压缩 + DP)

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

HDU 1074 Doing Homework(状态压缩DP)

题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小 思路:二进制表示. 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<set> 7 #include<map> 8

HDU 1074 Doing Homework(状压DP)

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 r

hdu 3217 Health(状态压缩DP)

Health Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 527    Accepted Submission(s): 145 Problem Description Unfortunately YY gets ill, but he does not want to go to hospital. His girlfriend LM

hdu 4057 AC自动机+状态压缩dp

http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, and Dr. X wants to take some rabbits to Noah's Ark, or there are no rabbits any more.

HDU 3001 Travelling(状态压缩DP+三进制)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目大意:有n个城市,m条路,每条路都有一定的花费,可以从任意城市出发,每个城市不能经过两次以上,要求经过所有城市并且花费最少,求出最小花费. 解题思路:三进制的状态压缩DP,跟二进制还是有一点不一样的,因为三进制没有直接的位运算,还要自己先做处理利用num[i][j]记录数字i各位的三进制表示方便计算,其他的就跟二进制状态压缩没有太大区别了.还有注意: ①开始要将n个起点初始化,dp[bit

HDU 4511 (AC自动机+状态压缩DP)

题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2->N,但是某些段路径(注意不是某些条)是被禁止的.问从1->N的最短距离. 解题思路: AC自动机部分: 如果只是禁掉某些边,最短路算法加提前标记被禁的边即可. 但是本题是禁掉指定的路段,所以得边走边禁,需要一个在线算法. 所以使用AC自动机来压缩路段,如禁掉的路段是1->2->3,那么in

HDU 1074 Doing Homework(像缩进DP)

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 r

HDU 1074 Doing Homework 状压DP

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 r

HDU1074 Doing Homework 状态压缩dp

题目大意: 根据完成任务的截止时间,超时一天罚1分,求完成所有任务后的最小罚时 这里n最大为15,可以利用状态压缩来解决问题 1 /* 2 首先要明白的一点是状态1/0分别表示这件事做了还是没做 3 而1/0的位置表示这是哪一件事 4 比如说 5 可以表示为101,那么表示第一个和第三个任务已经完成 5 而dp[5]表示第一个和第三个任务完成所花费的最短时间 6 而状态5(101)是从状态1(001)或者状态4(100)转移过来的 7 也就是说我们总是可以通过一个较小的状态不断递推一个较大状态的