HDU 1074 Doing Homework ——(状态压缩DP)

  考虑到n只有15,那么状压DP即可。

  题目要求说输出字典序最小的答案的顺序,又考虑到题目给出的字符串本身字典序是递增的,那么枚举i的时候倒着来即可。因为在同样完成的情况下,后选字典序大的,小的字典序就会在前面,那么整体的字典序就会更小。代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <string>
 5 #include <iostream>
 6 #include <stack>
 7 using namespace std;
 8 const int inf = 0x3f3f3f3f;
 9
10 struct node
11 {
12     string name;
13     int cost,deadline;
14 }a[20];
15 struct state
16 {
17     int pre,now;
18     int t,score;
19 }dp[1<<16];
20
21 int main()
22 {
23     int T;
24     scanf("%d",&T);
25     while(T--)
26     {
27         int n;
28         scanf("%d",&n);
29         for(int i=0;i<n;i++) cin >> a[i].name >> a[i].deadline >> a[i].cost;
30         int all = 1 << n;
31         for(int mask=1;mask<all;mask++)
32         {
33             dp[mask].score = inf;
34             for(int i=n-1;i>=0;i--)
35             {
36                 if(mask & (1<<i))
37                 {
38                     int pre = mask - (1<<i);
39                     int add = dp[pre].t + a[i].cost - a[i].deadline;
40                     if(add < 0) add = 0;
41                     if(dp[pre].score + add < dp[mask].score)
42                     {
43                         dp[mask].score = dp[pre].score + add;
44                         dp[mask].pre = pre;
45                         dp[mask].now = i;
46                         dp[mask].t = dp[pre].t + a[i].cost;
47                     }
48                 }
49             }
50         }
51         int temp = all - 1;
52         printf("%d\n",dp[temp].score);
53         stack<int> S;
54         while(temp)
55         {
56             S.push(dp[temp].now);
57             temp = dp[temp].pre;
58         }
59         while(!S.empty())
60         {
61             int x = S.top(); S.pop();
62             cout << a[x].name << endl;
63         }
64     }
65     return 0;
66 }
时间: 2024-10-24 11:18:46

HDU 1074 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

HDU 1074 Doing Homework(状态压缩DP)

题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小 思路:二进制表示. 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<set> 7 #include<map> 8

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 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 4057 AC自动机+状态压缩dp

http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, and Dr. X wants to take some rabbits to Noah's Ark, or there are no rabbits any more.

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

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

HDU 4511 (AC自动机+状态压缩DP)

题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2->N,但是某些段路径(注意不是某些条)是被禁止的.问从1->N的最短距离. 解题思路: AC自动机部分: 如果只是禁掉某些边,最短路算法加提前标记被禁的边即可. 但是本题是禁掉指定的路段,所以得边走边禁,需要一个在线算法. 所以使用AC自动机来压缩路段,如禁掉的路段是1->2->3,那么in

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

题目大意: 根据完成任务的截止时间,超时一天罚1分,求完成所有任务后的最小罚时 这里n最大为15,可以利用状态压缩来解决问题 1 /* 2 首先要明白的一点是状态1/0分别表示这件事做了还是没做 3 而1/0的位置表示这是哪一件事 4 比如说 5 可以表示为101,那么表示第一个和第三个任务已经完成 5 而dp[5]表示第一个和第三个任务完成所花费的最短时间 6 而状态5(101)是从状态1(001)或者状态4(100)转移过来的 7 也就是说我们总是可以通过一个较小的状态不断递推一个较大状态的