HDU 1074 Doing Homework 状态压缩+DP .

本文提供了一道来自杭电OJ的1074号题目的解决方案,采用C++实现,通过动态规划算法求解最优任务安排,以最大化得分。文章包含完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
struct HomeWork
{
	char name[100+5];
	int deadline,costday;
}homework[15+5];
struct Node
{
	int time,parent,score,idxN;
}d[1<<15];
void Print(int state){
	if(state==0) return;
	Print(d[state].parent);
	cout<<homework[d[state].idxN].name<<endl;
}
int main(int argc, char const *argv[])
{
	int T; cin>>T;
	while(T--)
	{
		int n; cin>>n;
		for(int i=0;i<n;i++)
			scanf("%s%d%d",homework[i].name,&homework[i].deadline,&homework[i].costday);

		for(int state=1;state<(1<<n);state++)
		{
			d[state].score=INF;
			for(int idx=0;idx<n;idx++)
			{
				if(((1<<idx)&state)==0) continue;
				int pre=state-(1<<idx);
				int score=d[pre].time+homework[idx].costday-homework[idx].deadline;
				if(score<0) score=0;
				if (score+d[pre].score<=d[state].score)				
				{
					d[state].score=d[pre].score+score;
					d[state].parent=pre;
					d[state].idxN=idx;
					d[state].time=d[pre].time+homework[idx].costday;
				}

			}
		}
		cout<<d[(1<<n)-1].score<<endl;
		Print((1<<n)-1);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值