博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ---1074 Doing Homework[DP+状态压缩]
阅读量:7033 次
发布时间:2019-06-28

本文共 3235 字,大约阅读时间需要 10 分钟。

Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3150    Accepted Submission(s): 1203

Problem 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.
 

 

Author
Ignatius.L
 
 
 
 
 
 
 
 
 
 
第一次测试数据:

代码是看了一位大牛的,和OY一起慢慢调试总算搞清楚,还是很有手滑的,画了个图,便于理解。关于DP和状态压缩还是需要多练练,以前都没接触过!状态压缩很巧妙!
思路:说说第一个测试数据,总共有3门课,也就是总共有7状态,而3,5,6状态是建立在状态1,2,4上的,通过自下而上进行DP,保留每个状态中的四个记录,一直到最后一个状态。
code:
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 13 using namespace std; 14 15 const int INF = 1<<30; 16 const int N = 20; 17 18 struct work{ 19 char name[N]; //课程名字 20 int deadline; //截止时间 21 int use_time; //需要时间 22 }; 23 24 work data[N+1]; 25 26 struct state{ 27 int used_time; //已经使用时间 28 int reduce_score; //减掉的最小学分 29 int last_work; //此状态下前一个工作 30 int ith_work; //这个状态完成的最后一个工作 31 }; 32 33 state d[1<
=0; j--) 63 { 64 temp = (int)(1<
0?reduce:0; 82 //(没有完成这个作业的某个状态被扣的学分 + 在这个状态完成并被扣学分最小的情况下完成这个作业被扣的学分) < i状态下被扣学分,就更新 83 if (d[last].reduce_score + reduce < d[i].reduce_score) 84 { 85 d[i].reduce_score = d[last].reduce_score+reduce; 86 d[i].last_work = last; 87 d[i].ith_work = j; 88 d[i].used_time = d[last].used_time+data[j].use_time; 89 } 90 } 91 } 92 } 93 printf("%d\n", d[max_value-1].reduce_score); 94 int pos[N+1], k=0, x=max_value-1; 95 //递推得出序列 96 while (x > 0) 97 { 98 pos[k++] = d[x].ith_work; 99 x = d[x].last_work; 100 } 101 for (i=k-1; i>=0; i--) 102 printf("%s\n",data[pos[i]].name); 103 } 104 return 0; 105 }

 

 

转载地址:http://evyal.baihongyu.com/

你可能感兴趣的文章
BeanNameAutoProxy1
查看>>
Mysql存储过程(procedure)增加测试数据
查看>>
2017-02-24
查看>>
Vim 基本配置
查看>>
中文网页字体:网页设计师的字体替换方法...
查看>>
Linux下WebSphere安装
查看>>
django实现文件下载
查看>>
编译 Clozure CL 的 Mac IDE 版,超级简单
查看>>
Windows 下 gcc + golang 编译 git2go
查看>>
@Transactional数据事务控制
查看>>
juniper交换机ex2200配置(生产环境)
查看>>
SecureCRT 生成公钥KEY登录
查看>>
AOP原理
查看>>
ubuntu maven 安装配置
查看>>
医学教育网批量资源下载程序之——获取下载列表
查看>>
#CCNA#笔记第二弹
查看>>
控制面板打不开
查看>>
JVM 之 ParNew 和 CMS 日志分析
查看>>
开发者分享 | 从零开始开发一个即时通讯项目
查看>>
var 是 Java 开发的好朋友啊!
查看>>