DP第五题hdu1074 Doing Homework(状压)

Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12028 Accepted Submission(s): 5773

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.

介绍一下状压DP:
转载:https://blog.csdn.net/bentutut/article/details/70147989
状压dp

此dp可以理解为最暴力的dp,因为他需要遍历每个状态,所以将会出现2^n的情况数量,所以明显的标志就是数据不能太多(好像是<=16?),然后遍历所有状态的姿势就是用二进制来表示,01串,1表示使用,0表示未使用,就把所有的状态投射到很多二进制的数上(类似于hash?)然后对每个状态找上一”些”状态的方法如下代码,即状压dp的精髓!!!

for(s=1;s<(1<<n);s++)
{
     for(i = n-1 ; i >=0;i--)
    {       
        tem=1<<i;        //  1在某一位,其它位为0
        if(tem & s)         //判断是否能由此种状态达到,即判断当前位是1还是0
        {
            past=s-tem;     //当前位的1变为0即为上一状态
        }    
    }         
}

位运算操作:
这里写图片描述

转载:https://blog.csdn.net/libin56842/article/details/24316493

思路:因为最多只有15门课程,可以使用二进制来表示所有完成的状况

例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,那么我们可以枚举便可以 1~1 << n得出所有的状态,然后对于每一门而言,其状态是t = 1 << i,我们看这门在现在的状态s下是不是完成,可以通过判断s&t是否为1来得到,当得出t属于s状态的时候,我们便可以进行DP了,在DP的时候要记录路径,方便之后的输出

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
typedef struct Work{
    char s[105];
    int dead,num;
}Work;
Work a[16];
typedef struct Node{
    int time,score,pre,now;
}Node;
Node dp[1 << 15];

int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            int n;
            scanf("%d",&n);
            for(int i = 0;i < n;++i)
            {
                scanf("%s %d %d",a[i].s,&a[i].dead,&a[i].num);
            }
            memset(dp,0,sizeof(dp));
            for(int s = 1;s < (1 << n);++s)
            {
                dp[s].score = inf;
                for(int i = n - 1;i >= 0;--i)
                {
                    int tmp = 1 << i;
                    if(s & tmp)
                    {
                        //可以找到上一个状态
                        int past = s - tmp;
                        int st = dp[past].time + a[i].num - a[i].dead;
                        //能在规定期限内完成
                        if(st < 0)
                            st = 0;
                        if(st + dp[past].score < dp[s].score)
                        {
                            dp[s].score = dp[past].score + st;
                            dp[s].time = dp[past].time + a[i].num;
                            dp[s].now = i;
                            dp[s].pre = past;
                        }
                    }
                }
            }
            stack<int>sta;
            int ans = (1 << n) - 1;
            printf("%d\n",dp[ans].score);
            while(ans)
            {
                sta.push(dp[ans].now);
                ans = dp[ans].pre;
            }
            while(!sta.empty())
            {
                printf("%s\n",a[sta.top()].s);
                sta.pop();
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值