HDU 1074

题目 :http://acm.hdu.edu.cn/showproblem.php?pid=1074


DP类的状态压缩。这题其实就是求所有家庭作业的全排列,也就是最多有15!种放法,而 15!=1 307 674 368 00 所以暴力肯定会超时的。

这题的模型就是类似于数塔一样的。同时由于状态太多。所以利用二进制状态压缩。 


下面是AC代码 :

#include<iostream>
#include<string>
#include<stack>
using namespace std;
struct node{
	string name;           
	int d;                                  //截止日期
	int c;                                  //需要花费日期 
}hw[20];

struct point{
	int now,pre;                           //当前状态id,与过去状态id
	int now_time;                          //当前时间
    int score;                             //当前最少花费
	int key;                               //当前课程的id
	
}dp[40000];
int main(){
	int t,n,i,j;
	
	cin>>t;
	
	while(t--){
		cin>>n;
		for(i=0;i<n;i++)  cin>>hw[i].name>>hw[i].d>>hw[i].c;
		
		dp[0].now_time=0;  dp[0].score=0;   dp[0].now=0;
		
		for(i=1;i<(1<<(n));i++){
			dp[i].score=1000000000;
			for(j=n-1;j>=0;j--){
				if(i&(1<<j)){
					int pre_id=i-(1<<j),temp=0;
					
					if(dp[pre_id].now_time+hw[j].c>hw[j].d)
						temp=dp[pre_id].now_time+hw[j].c-hw[j].d;
					else temp=0;
					
					
					if(dp[pre_id].score+temp<dp[i].score){
						dp[i].score=dp[pre_id].score+temp;
						dp[i].now=i;
						dp[i].pre=pre_id;
						dp[i].now_time = dp[pre_id].now_time+hw[j].c;
						dp[i].key=j;
						
					}
				}
			}
			
		}
		
		
        stack<string >st;
		cout<<dp[(1<<n)-1].score<<endl;
		
		int t=(1<<(n))-1;
		
		while(dp[t].now!=0){
			
			st.push(hw[dp[t].key].name);
			t=dp[t].pre;
		//	cout<<t<<endl;
			
		}
		while(!st.empty()){
			cout<<st.top()<<endl;
			st.pop();
		}
		
		
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值