[HDU1074]Doing Homework (状压DP)

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.

InputThe 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.
OutputFor 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

DP姿势水平还是有待提高啊...对于dp[x],x的第i位为1则表示第i个作业已经完成,对于每个x枚举非零位j,如果dp[x] + (int)max(1ll*0,sumc - d[j])<dp[(1<<j)|x],说明从状态x完成j后到达
(1<<j)|x更优,则更新。维护del数组记录每个状态最近完成的作业是哪一个,然后通过dfs输出最优状态的完成顺序。
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <cstring>
 7 #define N 20
 8 #define pow2(_) (1<<_)
 9 #define inf 0x3f3f3f3f
10 #define D(_) cout << #_ << " " << _ << endl;
11 using namespace std;
12 int T,n,d[N],c[N];
13 char s[N][105];
14 int dp[pow2(N)],del[pow2(N)];
15 typedef long long ll;
16
17 void output(int n){
18     if (n == pow2(del[n])) {
19         printf("%s\n",s[del[n]]);
20         return;
21     }
22     output(n^pow2(del[n]));
23     printf("%s\n",s[del[n]]);
24 }
25
26 int main(){
27     scanf("%d",&T);
28     while(T--){
29         scanf("%d",&n);
30         for (int i = 0;i < n;++i){
31             scanf("%s %d %d",&s[i],&d[i],&c[i]);
32         }
33         memset(dp,0x3f3f3f3f,sizeof(dp));
34         dp[0] = 0;
35         int maxn = (1<<n)-1;
36         for (int i = 0;i < maxn;++i){
37             for (int j = 0;j < n;++j){
38                 if (pow2(j)&i) continue;
39                 ll sumc = c[j];
40                 for (int k = 0;k < n;++k){
41                     if (pow2(k)&i) sumc += c[k];
42                 }
43                 int tmp = dp[pow2(j)|i];
44                 dp[pow2(j)|i] = min(dp[pow2(j)|i],dp[i] + (int)max(1ll*0,sumc - d[j]));
45                 if (tmp != dp[pow2(j)|i]) del[pow2(j)|i] = j;
46             }
47         }
48
49         printf("%d\n",dp[pow2(n)-1]);
50         output(pow2(n)-1);
51     }
52 }


原文地址:https://www.cnblogs.com/mizersy/p/12288220.html

时间: 2024-08-01 01:41:41

[HDU1074]Doing Homework (状压DP)的相关文章

HDU 1074 Doing Homework(状压DP)

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 r

HDU 1074 Doing Homework 状压DP

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 r

HDU1074:Doing Homework(状压DP)

Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15868    Accepted Submission(s): 7718 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a l

状压DP [HDU 1074] Doing Homework

Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5472    Accepted Submission(s): 2311 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lo

HDU 1074:Doing Homework(状压DP)

http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7704    Accepted Submission(s): 3484 Problem Description Ignatius has just come bac

HDU 1074 Doing Homework (状压dp)

题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则按照作业字典序输出(注意:输入也是按照字典序输入的) 题解:首先想到的是暴力dfs,但是会超时.接着我们看到n最大只有15,因此可以使用状压dp,但是状态不能用位置表示 但我们可以这样:0表示此作业没有做过,1表示已经用过了,接着遍历0->(1<<n)-1贪心(例如:3(011)可以找2(0

HDU 1074 Doing Homework DP 状压DP

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目描述: 给你所有课程的截止时间和工作时长, 一次只能做一种作业, 问最少罚时几天 N <= 15 解题思路: 由于N很小, 所以第一反应就是状压DP, 我们可以用一个15位二进制数来表示各个课程做完还是没做完, 然后从 S 从 1 到 1 << N 枚举 i 从 1 到 N 枚举, 如果S & (1<<i) 有效则说明i 属于情况 S, 这样我们从上一步S -

【状压DP】【HDOJ1074】

http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12269    Accepted Submission(s): 5911 Problem Description Ignatius has just come bac

ZOJ3305Get Sauce 状压DP,

状压DP的题目留个纪念,首先题意一开始读错了,搞了好久,然后弄好了,觉得DFS可以,最后超时,修改了很久还是超时,没办法看了一下n的范围,然后觉得状压可以,但是没有直接推出来,就记忆化搜索了一下,可是一直错,莫名奇妙,然后没办法看了一下题解,发现了下面这个比较好的方法,然后按照这个方程去推,然后敲,也是WA了好多把,写的太搓了,没人家的清楚明了,唉~也算是给自己留个纪念,状压一直做的都不太好~唉~还好理解了, 参考了  http://blog.csdn.net/nash142857/articl