hdoj 1074 doing homework

//n<=15,由题意知,只需对这n份作业进行全排列,选出扣分最少的即可。 //用全排肯定会超时,o(n!),当然,我试过,超时了。 //用一个二进制数存储这n份作业的完成情况,第1.。。。n个作业状况分别 //对应二进制数的第0,1.。。。。,n-1位则由题意,故数字上限为2^n //其中 2^n-1即为n项作业全部完成,0为没有作业完成。。。 //用dp[i]记录完成作业状态为i时的信息(所需时间,前一个状态,最少损失的分数)。 //递推条件如下 //1.状态a能做第i号作业的条件是a中作业i尚未完成,即a&i=0。 //2.若有两个状态dp[a],dp[b]都能到达dp[i],那么选择能使到达i扣分小的那一条路径,若分数相同,转入3 //3.这两种状态扣的分数相同,那么选择字典序小的,由于作业按字典序输入,故即dp[i].pre = min(a,b); //初始化:dp[0].cost = 0;dp[0].pre=-1;dp[0].reduced = 0; //最后dp[2^n-1].reduced即为最少扣分,课程安排可递归的输出 #include <iostream> #include <cstring> #include <string> using namespace std; struct node { string name; int deadline; int cost; }data[15]; struct { int _time; int pre; int reduce; int num; }dp[1 << 15]; int n; bool isVis[1 << 15]; void output(int cur) { if(cur == 0 || cur == -1) return; output(dp[cur].pre); cout << data[dp[cur].num].name << endl; } int main() { //freopen("1.txt", "r", stdin); int t; cin >> t; while(t--) { cin >> n; for(int i = 0; i < n; i++) { cin >> data[i].name >> data[i].deadline >> data[i].cost; } memset(isVis, false, sizeof(isVis)); dp[0].pre = -1; dp[0]._time = dp[0].reduce = 0; isVis[0] = true; for(int i = 0; i < (1 << n) - 1; i++) { //遍历所有状态 for(int j = 0; j < n; j++) { int cur = 1 << j; if((cur & j) == 0) { //郁闷。。调试半天发意识到优先级问题 int curTmp = cur | i; int _time = dp[i]._time + data[j].cost; int _reduce = dp[i].reduce; if(_time > data[j].deadline) _reduce += _time - data[j].deadline; if(isVis[curTmp]) { if(_reduce < dp[curTmp].reduce) { dp[curTmp].reduce = _reduce; dp[curTmp].pre = i; dp[curTmp]._time = _time; dp[curTmp].num = j; } }else { isVis[curTmp] = true; dp[curTmp].reduce = _reduce; dp[curTmp].pre = i; dp[curTmp]._time = _time; dp[curTmp].num = j; } } } } cout << dp[(1 << n) - 1].reduce << endl; output((1 << n) - 1); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值