状态压缩DP,附上例题(HDU - 1074 Doing Homework )

状态压缩DP:本生有很多状态,然后压缩成一种状态。

很多时候都会使用位运算

Doing Homework

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

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.

题解:有n种作业,将作业做完,损失的分数最小是多少,并将作业按顺序输出

用二进制的方法表示作业的完成情况,比如4个作业完成情况是1010,1代表完成,0代表没有完成,所以作业的总完成量为2.

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7 int const INF = 0x3f3f3f3f;
 8 int const MAX = (1<<15) + 10;
 9 int pre[MAX],dp[MAX], t[MAX];
10 char s[20][110];
11
12 void print(int n) {//存储时候从后往前,所以输出时,先递归进行输出,从后往前输出
13     if(n == 0) return ;
14     print(n-(1 << pre[n]));
15     printf("%s\n",s[pre[n]]);
16 }
17 int main()
18 {
19     int T,n;
20     int dl[20],dr[20];
21     while(scanf("%d",&T) != EOF) {
22         while(T--){
23             scanf("%d",&n);
24             for(int i = 0;i < n; i++)
25                 scanf("%s%d%d",s[i],&dl[i],&dr[i]);
26             int w = 1<<n;
27             for(int i = 1; i < w; i++) {//从1种到1<<n种情况进行枚举
28                 dp[i] = INF;
29                 for(int j = n-1 ; j >=0 ;j--) {//用二进制表示选用第几个的情况
30                     int cnt = 1 << j;
31                     if( !(i&cnt))continue;//没有选用第j个
32                     int score = t[i - cnt] - dl[j] + dr[j];
33                     if(score < 0) score = 0;//分数小于0说明没有超出时限
34                     if(dp[i] > dp[i-cnt] + score) {
35                         dp[i] = dp[i-cnt] +score;
36                         t[i] = t[i-cnt] + dr[j];
37                         pre[i] = j;
38                     }
39                 }
40             }
41             printf("%d\n",dp[w-1]);
42             print(w-1);
43         }
44     }
45     return 0;
46 }
时间: 2024-10-14 09:50:16

状态压缩DP,附上例题(HDU - 1074 Doing Homework )的相关文章

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&#183;状态压缩)

题意  有n个作业要做   给你每个作业的最后期限  和做完这个作业需要的时间  作业每超过最后期限一天就会扣一分  只能把一个作业做完了再做另一个作业  问做完所有作业至少扣多少分 作业最多只有15个  看到这个数字容易想到是状态压缩  dp[i]表示i对应状态的最小扣分  i转换为二进制后为1的位表明该位对应的作业已经做了  为0的位没做  那么dp[i] = min{dp[k] + cost | k为将某一位变成1后等于 i 的状态} 由于要打印路径  所有还需要记录每个状态的上一个状态p

hdu 4568(状态压缩dp)

题意:一张n*m的网格内每个点有话费,还有若干个宝藏,问一个人要走进去拿走所有宝藏在走出来的最小花费. 思路:看宝藏只有13个直接想到了状压dp[i][j]拿了哪几个前一个为j的最小花费,先bfs+优先队列预处理出最短路,然后记忆化搜索就可. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao

HDU 1565 方格取数(1) (状态压缩DP)

HDU 1565 方格取数(1) (状态压缩DP) ACM 题目地址: HDU 1565 方格取数(1) 题意: 中文. 分析: dp[i][j]表示前i行状态j的最优解. 先预处理出符合条件的数,17000+个(n在20以内). 不过感觉复杂度挺高的会T,但是却能A. 这题的正解应该是最小割,回头补下. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: 1565_dp.cpp * Create Date: 2014-09-19 23

hdu 3217 Health(状态压缩DP)

Health Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 527    Accepted Submission(s): 145 Problem Description Unfortunately YY gets ill, but he does not want to go to hospital. His girlfriend LM

HDU 3001 Travelling (三进制状态压缩 DP)

题意:有 n 个city,可以选择任一城市作为起点,每个城市不能访问超过2次, 城市之间有权值,问访问全部n个城市需要的最小权值. 思路:因为每个城市可以访问最多两次,所以用三进制表示访问的状态. 详细见代码注释!!!! #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #inclu

HDU 5135 Little Zu Chongzhi&#39;s Triangles(状态压缩dp+Vector)

这道题是水题,当时直接贪心就过了. 多阶段决策,其实应该用dp,他人的代码使用Vector进行预处理. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; int n, a[12]; double dp[1<<12]; double cal(int a, int b, i

HDU 3001 Travelling(状态压缩DP+三进制)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目大意:有n个城市,m条路,每条路都有一定的花费,可以从任意城市出发,每个城市不能经过两次以上,要求经过所有城市并且花费最少,求出最小花费. 解题思路:三进制的状态压缩DP,跟二进制还是有一点不一样的,因为三进制没有直接的位运算,还要自己先做处理利用num[i][j]记录数字i各位的三进制表示方便计算,其他的就跟二进制状态压缩没有太大区别了.还有注意: ①开始要将n个起点初始化,dp[bit

HDU 4539 郑厂长系列故事——排兵布阵 (状态压缩DP)

中文题,题意不再累赘. 思路:对于第 i 行的放士兵,影响它的只有第 i-1 行和 i-2 行,所以暴力枚举符合这三行的状态 state[i],state[j],state[k].  接下来就是二进制的巧妙应用了. 具体题解看代码注释!!! #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath&