Hduoj1074【DP状态压缩】

/*Doing Homework
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5910    Accepted Submission(s): 2502

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

Recommend
We have carefully selected several similar problems for you:  1024 1078 1080 1158 1227 
*/
#include<stdio.h>
#define INF 100000000
const int  MAX = (1<<15)+10;
int n, dp[MAX], t[MAX], pre[MAX], dea[MAX], cost[MAX];//dp记录状态i所扣的最小分数t代表状态i下所花费的时间
char s[18][110]; 
void out(int x)
{
	if(!x)//如果x为0则结束 
	return ;
	out(x - (1<<pre[x]));//递归输出前面的 当前状态 - 前驱未完成的状态  即为前面一个状态 
	printf("%s\n", s[pre[x]]);//再输出后面的保证从前往后输出 
}
int main()
{
	int i, j, k, T;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		for(i = 0; i < n; ++i)
		scanf("%s%d%d", s[i], &dea[i], &cost[i]);
		int bit = 1<<n;//状态的总数
		for(i = 1; i < bit; ++i)//枚举
		{
			dp[i] = INF;//初始化
			for(j = n-1; j >= 0; --j)
			{
				int temp = 1<<j;//第j个作业的状态
				if(!(i&temp) )
				continue;//如果状态i中第j个作业是未完成的则结束
				int num = t[i-temp] + cost[j] - dea[j];//计算第j项作业未完成的状态到现在完成所需要扣的分数
				if(num < 0)
				num = 0;//如果不扣分
				if(dp[i] > dp[i-temp] + num)
				{
					dp[i] = dp[i-temp] + num;//记录最小的扣分
					t[i] = t[i-temp] + cost[j];//记录到该状态花费的天数
					pre[i] = j;//记录最小花费的前驱 
				} 
			} 
		} 
		printf("%d\n", dp[bit-1]);
		out(bit-1);//递归输出路径 
	}
	return 0;
}


题意:给出n项作业,每一项作业都有一个期限和完成这项作业需要的天数,当完成这项作业的时间每超过一天就会扣一分,现在求怎样安排完成作业的顺序可以使所扣的分数最小。

思路:我纯属参考过来的,大家可以看看大神的解答http://blog.csdn.net/xingyeyongheng/article/details/21742341。

你可能感兴趣的:(Hduoj1074【DP状态压缩】)