zoukankan      html  css  js  c++  java
  • HDU1074(状态压缩DP)

    Doing Homework

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7070    Accepted Submission(s): 3104


    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
    枚举会TLE,状态压缩DP,copy大神的:http://blog.csdn.net/y990041769/article/details/39546633
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int MAXN=16;
    const int INF=0x3fffffff;
    struct Node{
        char s[105];
        int time,cost;
        Node()
        {
            memset(s,0,sizeof(s));
        }
    }a[MAXN];
    int n;
    int dp[1<<MAXN];
    int pre[1<<MAXN];
    void OutPut(int status)
    {
        if(status==0)
            return ;
        int t=0;
        for(int i=0;i<n;i++)
            if((status&(1<<i))!=0&&(pre[status]&(1<<i))==0)
            {
                t=i;
                break;
            }
        OutPut(pre[status]);
        printf("%s
    ",a[t].s);
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {    
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%s",a[i].s);
                scanf("%d%d",&a[i].time,&a[i].cost);
            }    
            for(int i=0;i<(1<<MAXN);i++)
                dp[i]=INF;
            memset(pre,0,sizeof(pre));
            dp[0]=0;
            for(int st=0;st<(1<<n);st++)
            {
                int w=0;
                for(int i=0;i<n;i++)
                {
                    if(st&(1<<i))
                        w+=a[i].cost;
                }
                
                for(int i=0;i<n;i++)
                {
                    if((st&(1<<i))==0)
                    {
                        if(dp[st|(1<<i)]>dp[st]+max(0,w+a[i].cost-a[i].time))
                        {
                            dp[st|(1<<i)]=dp[st]+max(0,w+a[i].cost-a[i].time);
                            pre[st|(1<<i)]=st;
                        }
                    }    
                }
            }
            printf("%d
    ",dp[(1<<n)-1]);
            OutPut((1<<n)-1);
        }
        
        return 0;
    }
  • 相关阅读:
    HashMap源码分析
    Vector和Stack源码分析/List集合的总结
    LinkedList源码分析
    ArrayList源码分析
    第三章 数据链路层(三)
    Java常考面试题(五)
    hibernate(一) 第一个hibernate工程
    回想过去,展望未来
    “Cannot load php5apache2_4.dll into server”问题的解决方法
    win7下80端口被(Pid=4)占用的解决方法
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5224200.html
Copyright © 2011-2022 走看看