杭电hdu 1074 Doing Homework dp

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

动态压缩dp,感觉还是没有怎么懂,主要用的是二进制的位运算。有待于研究。

//动态压缩dp
#include <stdio.h>
#include <iostream>
#include <cstring>

using namespace std;

typedef struct _homework
{
	char name[102];
	int deadline;
	int needday;
}homework;

homework hwk[16];
int n;

typedef struct _DP
{
	int ndday;
	int pre;
	int lowcost;
}DP;

DP dp[1<<15];
bool visited[1<<15];

void output(int status)
{
	int curwk = dp[status].pre ^ status;
	int curid = 0;
	curwk >>= 1;
	while(curwk){
		curid ++;
		curwk >>= 1;
	}
	if(dp[status].pre != 0){
		output(dp[status].pre);
	}
	printf("%s\n", hwk[curid].name);
}

int main()
{
	freopen("input.txt", "r", stdin);
	int t, i, j;
	int upper, cur, curtmp, reduce;
	scanf("%d", &t);
	while(t --){
		scanf("%d", &n);
		for(i = 0; i < n; i ++){
			scanf("%s%d%d", hwk[i].name, &hwk[i].deadline, &hwk[i].needday);
		}
		memset(visited, false, sizeof(visited));
		upper = (1 << n) - 1;//最大
		dp[0].ndday = 0, dp[0].lowcost = 0, dp[0].pre = -1;
		visited[0] = true;
		for(i = 0; i < upper; i ++){
			for(j = 0; j < n; j ++){
				cur = 1 << j;
				if((cur & i) == 0){
					curtmp = cur | i;
					dp[curtmp].lowcost = dp[i].lowcost + hwk[j].needday;
					reduce = dp[curtmp].lowcost - hwk[j].deadline;
					if(reduce < 0)reduce = 0;
					reduce += dp[i].ndday;
					if(visited[curtmp]){
						if(reduce < dp[curtmp].ndday){
							dp[curtmp].ndday = reduce;
							dp[curtmp].pre = i;
						}
					}
					else {
						visited[curtmp] = true;
						dp[curtmp].ndday = reduce;
						dp[curtmp].pre = i;
					}//else
				}//if
			}//for
		}//for
		printf("%d\n", dp[upper].ndday);
		output(upper);
	}//while
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值