HDU1074 Doing Homework(状态压缩DP)
2016-05-19 17:27
477 查看
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7618 Accepted Submission(s): 3425
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
[align=left]Sample Output[/align]
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.
题意:有N个作业,每个作业有结束时间和消耗时间,完成某个作业如果超出限定时间就会扣分。思路:N最大只有15,可以有二进制表示当前作业完成状态,进行dp。
#include <stdio.h> #include <string.h> #include <algorithm> #define maxn 1<<15 using namespace std; struct node{ int pre; int cost; int cut; }dp[maxn]; struct course{ char name[110]; int e, need; }a[20]; bool vis[maxn]; void output(int now){ int last = now^dp[now].pre; int cnt = 0; last>>=1; while(last != 0){ cnt++; last>>=1; } if(dp[now].pre != 0) output(dp[now].pre); printf("%s\n", a[cnt].name); } int main() { int T, i, j, N; scanf("%d", &T); while(T--) { scanf("%d", &N); for(int i = 0;i < N;i++) scanf("%s %d %d", a[i].name, &a[i].e, &a[i].need); memset(vis, 0, sizeof vis); dp[0].pre = -1; dp[0].cost = 0; dp[0].cut = 0; vis[0] = 1; for(i = 0;i < ((1<<N)-1);i++){ for(j = 0;j < N;j++){ if((i&(1<<j)) != 0) continue;//已经完成j,跳过 int now = i|(1<<j); int sum = dp[i].cost + a[j].need;//总时间 int rde = sum - a[j].e;//扣分 if(rde < 0) rde = 0; rde += dp[i].cut; if(vis[now]){ if(rde < dp[now].cut){ dp[now].cut = rde; dp[now].pre = i; dp[now].cost = sum; } }else{ vis[now] = 1; dp[now].cut = rde; dp[now].pre = i; dp[now].cost = sum; } } } printf("%d\n", dp[(1<<N)-1].cut); output((1<<N)-1); } }
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- HDU 1568
- HDU1290
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp