hdu-1074 Doing Homework [状态dp]

D - Doing Homework
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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. 
         
 
这道题网上讲解很多,我就直接代码奉上了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#define MAX_SIZE 20
#define MAX_STATUS 1<<17
using namespace std;
struct subject{
	int deadline,cost;
};
vector<subject> sj;
int dp[MAX_STATUS];
int pre[MAX_STATUS];//记录当前状态的前一个状态
char in_str[MAX_SIZE][MAX_SIZE];
void init()
{//初始化
	sj.clear();
	pre[0]=-1;
	memset(dp,0x3f,sizeof(dp));
}
void input(int n)
{//题目给出 读入字符串序列的是字典序
	int deadline,cost;
	for(int i=0;i<n;++i)
	{
		scanf("%s %d %d",&in_str[i],&deadline,&cost);
		sj.push_back((subject){deadline,cost});
	}
}
void solve(int n)
{
	int end =(1<<n)-1;//结束状态为n个1
	dp[0]=0;
	for(int i=0;i<end;++i)
	{/*顺序遍历所有状态。可以画来看看,每个状态
		的前驱都访问到了,这个状态才会访问到它的转移目标,所以不用担心
		顺序遍历会跳层
		*/
		for(int j=0;j<n;++j)//做下一个作业
			if(!((1<<j)&i))
			{
				int cost=0;//总共花费时间
				int next=(1<<j)|i;//下一个状态
				for(int k=0;k<n;++k)
					if((1<<k)&next)
						cost+=sj[k].cost;
				//计算扣分
				int reduced=(cost<=sj[j].deadline?0:cost-sj[j].deadline);
				//printf("i:%d dp:%d re:%d next:%d\n",i,dp[i],reduced,next);
				if(dp[next]>dp[i]+reduced)
				{
					dp[next]=dp[i]+reduced;
					pre[next]=i;
				}
			}
	}
	printf("%d\n",dp[end]);
	//寻根存值
	stack<int> sta;
	for(int s=end;s!=0;s=pre[s])
		sta.push(s^pre[s]);
	while(!sta.empty())
	{
		int print=sta.top();sta.pop();
		for(int i=0;i<n;++i)
			if((1<<i)&print)
				printf("%s\n",in_str[i]);
	}
}
int main()
{
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		init();
		scanf("%d",&n);
		input(n);
		solve(n);
	}
	return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值