HDU 1074 状态压缩DP入门(1)

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门课的作业,每一个作业有自己的截止时间和完成需要的花费,如果有一门课过了自己的截止时间的话,一天会在期末总成绩上扣1分,然后让你怎么安排这个补作业的时间,使得自己期末成绩扣的分数最少。


解题思路: 这是一道简单的状态压缩DP的题,之前做过,但是没怎么好好巩固状态压缩DP的知识,所以现在不会,从基础做起。曾经暑假的时候去高校学习的时候,也接触过状态压缩,但是没怎么掌握。今天有幸有了突破。

对于状态压缩DP,我想总结一句话: 先表示状态,然后再细化顺序。 0表示不取,1表示取。然后这就会构成很多二进制来表示每一种情况。比如有三种物品的话,用二进制来表示的话 有 001 010 011 100 101 110 111(7种情况)。然后我们可以先取第1个物品,然后再取第三个物品,最后再取第二个物品。这得根据题目具体情况来决定。这个规则对于这道题来说,是一个不错的总结。 这也算是我最近学到掌握的新技巧吧。(博主还是很菜的,求勿喷。)


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack> 
using namespace std;
const int maxn=20;
const int inf=0x3f3f3f3f;
int n,t;
struct Node{
	char ch[110];
	int cost,dead;	
}node[maxn];
struct no{
	int score,now,pre,time;
}dp[1<<15];
int main(){
	int i,j;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%s%d%d",node[i].ch,&node[i].dead,&node[i].cost);
		}
		int end=1<<n;
		for(i=1;i<end;i++){
			dp[i].score=inf;
			for(j=n-1;j>=0;j--){
				int s=1<<j;
				if(i&s){
					int k=i-s;
					int st=dp[k].time+node[j].cost-node[j].dead;
					if(st<0) st=0;
					if(st+dp[k].score<dp[i].score){
						dp[i].score=st+dp[k].score;
						dp[i].pre=k;
						dp[i].now=j;
						dp[i].time=dp[k].time+node[j].cost;
					}
				}
			}
		}
		int star=end-1;
		stack<int> ve;
		while(star){
			ve.push(dp[star].now);
			star=dp[star].pre;
		}
		printf("%d\n",dp[end-1].score);
		while(!ve.empty()){
			int k=ve.top();
			ve.pop();
			printf("%s\n",node[k].ch);
		}
	}
	return 0;
}



今天逛杭电的时候,看到了一句好话:不拼搏,枉少年。(送给路过的你们,加油。)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值