zoukankan      html  css  js  c++  java
  • HDU 1074 Doing Homework (动态规划,位运算)

    HDU 1074 Doing Homework (动态规划,位运算)

    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

    Http

    HDU:https://vjudge.net/problem/HDU-1074

    Source

    动态规划,位运算,位压缩

    题目大意

    给定一些作业,每一种作业都有完成需要花费的时间(Costtime)和最晚完成时间(Deadline),作业每晚完成一种作业一天,就要减去一点学分,现在求如何安排做这些作业的顺序,使得减去的学分最少

    解决思路

    看到n的范围15,再结合本题是集合上的动态规划问题,我们可以想到用位运算来维护。为了方便操作,本题所有的数组均从0开始命名,那么n个物品就存放在0~(n-1)里。
    那么我们用二进制来表示某项作业是否做了。首先可以知道的就是,初始状态是(假设以5件物品为例)00000(0),而最终状态就是11111((2^5-1))。那么我们从0开始枚举每一种组合状态,然后以此减去这个状态中的1来找到它可以从哪个状态转移过来。比如10111可以从00111,10011,10101,10110转移过来。那么我们要维护那些信息呢?对于每一种组合i我们需要维护其最小的学分损失(F[i])和要做到这个最小的损失的当前最后一项作业完成的时间(Nowtime[i]),转移方程就是(设j是我们找到的可以转移过来的状态,而k是从j转移到i新做了哪项作业)
    (F[i]=min(F[j]+max(Nowtime[j]+Costtime[k]-Deadline[k],0)))
    为什么要与0取max呢?因为完成这项作业时不一定到了这项作业的最晚完成时间,所以这时Nowtime[j]+Costtime[k]-Deadline[k]是负数,而F应该增加的是0(因为没有损失学分),所以要这样做,表示至少就是0。
    至于如何输出路径呢?我们可以记一个Path[i]表示组合状态i是做了那一项作业转移过来的,如i=1101,Path[i]=1就表示i是从1100转移过来的。
    另外需要注意的就是,本题要求按照字典序输出,而因为其输入数据已经保证了字典序,所以是要按照输入的优先顺序输出。那么处理这个就是设置一下枚举顺序,让我们枚举新做哪一个作业的编号尽量靠后,也就是代码中的j从大到小。比如10111从10011和00111转移过来代价是一样的那么我们选择00111(这里需要理解一下)。

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int maxN=20;
    const int inf=2147483647;
    
    int n;
    char Name[maxN][200];
    int Deadline[maxN];
    int Costtime[maxN];
    int F[1<<16];
    int Nowtime[1<<16];
    int Path[1<<16];
    
    void outp(int x);
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            scanf("%d",&n);
            for (int i=0;i<n;i++)
                cin>>Name[i]>>Deadline[i]>>Costtime[i];
            int MAX=1<<n;//MAX就是最终状态+1
            memset(F,127,sizeof(F));
            memset(Nowtime,0,sizeof(Nowtime));
            F[0]=0;//初始状态
            Nowtime[0]=0;
            Path[0]=-1;
            for (int i=1;i<MAX;i++)//枚举每一种状态
            {
                for (int j=n-1;j>=0;j--)//为了保证字典序,这里要从大到小,因为要让这个新做的作业尽量靠后
                {
                    int now=1<<j;
                    if ((i&now)==0)//如果这个状态不要求做第j个作业(也就是i的这一位为0),则不会从其转移过来
                        continue;
                    //cout<<i<<" "<<j<<endl;
                    now=now^i;//这里的now就变成了我们要找的可以转移到i的前面的状态
                    int delta=max(Nowtime[now]+Costtime[j]-Deadline[j],0);//delta就是计算如果做这门作业损失的学分是多少
                    if (F[now]+delta<F[i])//更新最优解,并记录信息
                    {
                        F[i]=F[now]+delta;
                        Nowtime[i]=Nowtime[now]+Costtime[j];
                        Path[i]=j;
                    }
                }
            }
            printf("%d
    ",F[MAX-1]);//输出
            outp(MAX-1);//输出方案
        }
        return 0;
    }
    
    void outp(int x)//递归输出方案
    {
        int k=Path[x];
        if (k!=-1)
            outp(x^(1<<k));
    	if (k==-1)
    		return;
        printf("%s
    ",Name[k]);
        return;
    }
    
  • 相关阅读:
    【SpringBoot框架学习】yml/yaml语法 详解
    【SpringBoot框架学习】搭建开发环境 详解
    【SpringBoot框架学习】热部署 的配置 详解
    JVM-类加载机制
    JVM-字节码
    JVM-垃圾收集
    JVM-体系结构
    HTTP-报文结构
    TCP-四次挥手
    TCP-三次握手
  • 原文地址:https://www.cnblogs.com/SYCstudio/p/7422855.html
Copyright © 2011-2022 走看看