每日三题-Day2-A(HDU 1074 Doing Homework 状压DP)
来源:互联网 发布:淘宝申请退货 多长时间 编辑:程序博客网 时间:2024/06/10 02:21
原题地址
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8928 Accepted Submission(s): 4181
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.
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
2ComputerMathEnglish3ComputerEnglishMathHintIn 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个作业,以及完成它所需时间和最晚昨晚时间。每门作业如果晚交一天扣一分,问如何安排做作业顺序使得所扣分数最少。如果最少分数有多种方案,字典序小的科目优先。
输出最少扣分,然后按做作业顺序输出科目名字。
注意,科目是按字典序输入的。
解题思路:
n<15,只要对完成科目的顺序全排列一下,找到扣分最小的便可。
使用状态压缩DP可快速求解。
AC代码:
#include<cstdio>#include<algorithm>using namespace std;#define N 1<<15#define INF 0x3f3f3f3fstruct DP{ int cost; int pre; int reduce; int w; int flag;} dp[N];struct homework{ int name[111]; int time; int deadline;} hw[20];void dfs(int a,int n){ if(dp[a].pre==-1) return ; else dfs(dp[a].pre,n); printf("%s\n",hw[dp[a].w].name); return ;}int main(){ int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%s%d%d",hw[i].name,&hw[i].deadline,&hw[i].time); } dp[0].cost=0; dp[0].reduce=0; dp[0].pre=-1; for(int i=1; i<(1<<n); i++) { dp[i].flag=0; } for(int i=0; i<(1<<n); i++) { for(int j=0; j<n; j++) { if(!(i&(1<<j))) { int sta = i|(1<<j); int tmp=dp[i].reduce+max(dp[i].cost+hw[j].time-hw[j].deadline,0); if(dp[sta].flag==1) { if(dp[sta].reduce>tmp) { dp[sta].cost=dp[i].cost+hw[j].time; dp[sta].pre = i; dp[sta].w = j; dp[sta].reduce = tmp; } } else { dp[sta].cost=dp[i].cost+hw[j].time; dp[sta].pre = i; dp[sta].w = j; dp[sta].reduce = tmp; dp[sta].flag=1; } } } } int p=(1<<n)-1; printf("%d\n",dp[p].reduce); /* for(int i=0;i<=7;i++) printf("%d %d %d %d %d\n",dp[i].cost,dp[i].reduce,dp[i].pre,dp[i].w,dp[i].flag); */ int s=0; dfs((1<<n)-1,n); } return 0;}
0 0
- 每日三题-Day2-A(HDU 1074 Doing Homework 状压DP)
- hdu 1074 Doing Homework (状压dp)
- HDU 1074 Doing Homework(状压DP)
- HDU 1074-Doing Homework(状压DP)
- HDU 1074 Doing Homework(状压dp)
- HDU 1074 Doing Homework(状压DP)
- HDU 1074 Doing Homework(状压DP)
- hdu 1074 Doing Homework(状压DP)
- HDU 1074 Doing Homework(状压DP)
- hdu-1074 Doing Homework (状压dp)
- Hdu 1074Doing Homework(状压DP)
- HDU 1074 Doing Homework (状压+DP) 初学dp笔记
- hdu 1074 Doing Homework (状压dp)
- HDU 1074 Doing Homework(状压DP)
- HDU 1074Doing Homework 状压Dp
- hdu 1074 Doing Homework 状压dp
- HDU 1074 Doing Homework (状压dp)
- Hdu 1074 Doing Homework 状压dp
- 关于Mac系统下adb安装以及DDMS中data目录的权限获取
- 如何生成.aar包和如何在项目中引入aar包?
- hive常用函数
- 深度优先搜索dfs之1到n的全排列
- Redis-集群监控之Redis monitor
- 每日三题-Day2-A(HDU 1074 Doing Homework 状压DP)
- hdu 1326 Box of Bricks
- 关于base64编码的一些理解
- 博弈论 sg函数
- KNN算法理解
- Debug与Release版本的区别详解
- 1017. A除以B
- 【LeetCode101-110】二叉树对称及存储,前序中序遍历生成二叉树,中序后序生成二叉树,数组转化为AVL平衡树,判断二叉树是否平衡
- AutoCompleteExtender返回text,value