HDU1074(状态压缩DP)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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\n",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\n",dp[(1<<n)-1]);
        OutPut((1<<n)-1);
    }
    
    return 0;
}

 

以上是关于HDU1074(状态压缩DP)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1074 Doing Homework ——(状态压缩DP)

HDU 1074 Doing Homework(状态压缩DP)

状态压缩DP,附上例题(HDU - 1074 Doing Homework )

HDU 1074 Doing Homework 状态压缩DP

HDU 1074 状态压缩DP 第一次写 多多指教

状态压缩 HDU1074