zoukankan      html  css  js  c++  java
  • HDU 1074-Doing Homework(状态压缩dp)

                                                                                                             Doing Homework       
    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
    题意:主人公有N门功课要做,每门功课做完需要一定的时间,而且每门功课有一个最后期限,如果该门功课延后一天交就得扣一分,而且每做一门功课主人公就一定把它做完为止,不会中途停下来再去做其他的。问怎样安排可使扣的分最少,如果有多组解,输出字典序最小的。
     
    解析:由于N很小,可以考虑dp状态压缩搜索,如果选了第i门功课,则二进制对应的位变为1,直到全部选完,更新最小值。打印时可以根据
    dp[]保存的值去找,比如当前状态为now,第i门功课没选,如果dp[now]==dp[now+(1<<i)]+GetTime(...),则可以选它,再往下搜,直到找到所有的解。
     
    代码如下:
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<vector>
    #include<iterator>
    #include<utility>
    #include<sstream>
    #include<iostream>
    #include<cmath>
    #include<stack>
    using namespace std;
    const int INF=1000000007;
    const double eps=0.00000001;
    int N,deadline[15],day[15];   //最后期限,完成功课要花的时间
    vector<string> ans_name;
    string name[15];              
    int dp[1<<15];                
    int Get(int id,int pass)
    {
        if(pass+day[id]<=deadline[id])  return 0;   //如果在期限之前完成
        return pass+day[id]-deadline[id];
    }
    void get_ans(int pick,int date)
    {
        if(pick==((1<<N)-1))  return;
    
        for(int i=0;i<N;i++)
        {
            if(pick&(1<<i))  continue;
            if(dp[pick]==dp[pick+(1<<i)]+Get(i,date))   //找到符合条件的
            {
                ans_name.push_back(name[i]);
                get_ans(pick+(1<<i),date+day[i]);
                break;
            }
        }
        return;
    }
    int dfs(int pick,int date)    //pick代表当前已选的状态,date代表现在是第几天
    {
        if(pick==((1<<N)-1))  return dp[pick]=0;    //全部选完
        int& ret=dp[pick];
        if(ret!=-1)  return ret;             //记忆化
        ret=INF;
        for(int i=0;i<N;i++)
        {
            if(pick&(1<<i))  continue;
            ret=min(ret,dfs(pick+(1<<i),date+day[i])+Get(i,date));  //取最大值
        }
        return ret;
    
    }
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            cin>>N;
            for(int i=0;i<N;i++)  cin>>name[i]>>deadline[i]>>day[i];
            memset(dp,-1,sizeof(dp));
            int ans=dfs(0,0);   //得到最大值
            printf("%d
    ",ans);
            ans_name.clear();
            get_ans(0,0);        //保存答案
            for(int i=0;i<ans_name.size();i++)  cout<<ans_name[i]<<endl;
        }
        return 0;
    }
    
     
     
     
  • 相关阅读:
    python数据结构之图的实现方法
    大数据将如何颠覆信任危机
    大数据将如何颠覆信任危机
    JQuery的入门(二)
    递归思想
    Jquery的入门(一)
    如果让你写一个消息队列,该如何进行架构设计啊?
    如何解决消息队列的延时以及过期失效问题?消息队列满了以后怎么处理?有几百万消息持续积压 几小时.怎么解决?
    如何保证消息的顺序性?
    如何保证消息在传送的过程中不会丢失?(如何保证消息的可靠性传输?)
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/4744139.html
Copyright © 2011-2022 走看看