HDU1074 Doing Homework(状态压缩 + 输出最合理的取法)

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.

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最大不超过15,

我们可以将所有的状态用二进制来表示(每个作业的取法),二进制中第i个位置代表了作业i的状态(做了还是没做)。

这里以n=3为例,我们可以根据二进制中"1"的位置来获取作业的信息,形如下图,

有(000,001,010,011,100,101,110,111)2^3个状态,当然其中某些状态可能会重复,所以我们要记录下最优的那个。
在这里插入图片描述

那么我们观察上图和根据题意我们可以得出一个递推式如下:

dp[i|(1<<j)]=min(dp[i]+扣去的分数,dp[i|(1<<j)])

i|(1<<j)代表将二进制的i的第j个位置变为1,那么相当于做了第j门作业。

如果存在多个最优解,那该怎么输出其中是按字典序排列的那个呢?

题目中有一句 Note: All the subject names are given in the alphabet increasing order.

也就是说题目中给出的顺序一开始就是按照字典序来排序的,那么我们就自顶向下地进行dp,这样得到的最优解就必然是字典序的.
在这里插入图片描述

代码 + 具体解析

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 16;

struct node
{
    char name[110];
    int D,C;
}e[maxn];

int t,n,x,y,z,sum,mx,cnt,ans;
int dp[1<<maxn],pre[1<<maxn],a[maxn];
string s;

void dfs(int idx)       //输出取法的路径
{
    if(idx==0)return;
    int t=0;
    for(int i=0;i<n;i++)
    {
        if(((1<<i)&idx) && (pre[idx]&(1<<i))==0)        //找到这次状态中是做的哪一门作业,并记录下位置
        {
            t=i;
            break;
        }
    }
    dfs(pre[idx]);
    printf("%s\n",e[t].name);
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {    
        memset(dp,inf,sizeof dp);
        memset(pre,0,sizeof pre);
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s %d %d",&e[i].name,&e[i].D,&e[i].C);
        dp[0]=0;
        for(int i=0;i<(1<<n);i++)
        {
            for(int j=0;j<n;j++)    
            {
                if(i&(1<<j))continue;           //二进制的i中的j位置是1,说明做过这门作业了
                sum=0;
                for(int k=0;k<n;k++)            //计算之前状态的总耗时
                    if(i&(1<<k))
                        sum+=e[k].C;
                sum+=e[j].C;                    //再加上第j门作业的耗时
                if(sum>e[j].D)sum-=e[j].D;      //总耗时减去deadline得到应扣去的分数
                else sum=0;                     //如果小于deadline,那么不用扣去分数
                if(dp[i|(1<<j)]>dp[i]+sum)      //更新dp数据,并记录下前一个状态的位置
                {
                    dp[i|(1<<j)]=dp[i]+sum;
                    pre[i|(1<<j)]=i;
                }
            }
        }
        printf("%d\n",dp[(1<<n)-1]);
        dfs((1<<n)-1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值