博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1074——Doing Homework(状态压缩DP)
阅读量:4248 次
发布时间:2019-05-26

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

Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5238    Accepted Submission(s): 2190


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
23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3
 

Sample Output
2ComputerMathEnglish3ComputerEnglishMath   
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.
 
题意:

有n门课程作业,每门作业的截止时间为D,需要花费的时间为C,若作业不能按时完成,每超期1天扣1分。

这n门作业按课程的字典序先后输入
问完成这n门作业至少要扣多少分,并输出扣分最少的做作业顺序
PS:达到扣分最少的方案有多种,请输出字典序最小的那一组方案

分析:

给出的n<=15,所以可以枚举所有的状态。

用一个1<<n内的整数可以表示一个状态,例如n=3的所有状态就是 001,010,011,100,101,110,111,每一位的0和1,对应某项作业做没做。

这样可以枚举出所有的状态,剩下的就是状态之间的转移。

对于每个状态,都可以尝试完成还没有完成的作业,即把某位的0变成1. 如:001 -->101 ,001-->011

而最终还要输出作业的顺序,所以要开一个数组,在每次成功更新某个状态的同时更新路径的前驱。

然后将路径递归输出。

#include 
#include
#include
#include
#include
#include
#define INF 0x7fffffffusing namespace std;struct node{ char s[200]; int d,c;}w[20];int dp[1<<16],time[1<<16],pre[1<<16];void init(){ for(int i=0;i< 1<<16 ;i++) { dp[i]=INF, time[i]=0, pre[i]=-1; }}void output(int x){ if(x==0) return; output(x-(1<
w[j].d) //判断是否超过期限。 cost=time[i|k]-w[j].d; else cost=0; if(dp[i|k]>dp[i]+cost) //更新最少扣分,和路径前驱。 dp[i|k]=dp[i]+cost, pre[i|k]=j; } } printf("%d\n",dp[gal]); output(gal); } return 0;}

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

你可能感兴趣的文章
简单子查询
查看>>
联表查询
查看>>
关于WindowListener的使用
查看>>
关于KeyListener的简单使用
查看>>
关于鼠标移动监听接口:MouseMotionListener
查看>>
TCP/IP详解笔记(一)
查看>>
501. Find Mode in Binary Search Tree
查看>>
504. Base 7
查看>>
593. Valid Square
查看>>
494. Target Sum
查看>>
463. Island Perimeter
查看>>
TCP协议粗析
查看>>
653. Two Sum IV - Input is a BST
查看>>
spark rdd 和 DF 转换
查看>>
RDD 基础操作
查看>>
RDD基本操作(下)
查看>>
##########(python 解析参数方法 可用) Python optionParser模块的使用方法 #######
查看>>
org.apache.hadoop.io.compress系列1-认识解码器/编码器
查看>>
pyspark-combineByKey详解
查看>>
从原理到代码:大牛教你如何用 TensorFlow 亲手搭建一套图像识别模块 | AI 研习社
查看>>