HDU_1074_Doing Homework_状态压缩dp

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074

Doing Homework

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


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
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.
 
最开始想用贪心做,没能做出。看了题解说是状态压缩dp,看完题解自己写了一遍。
思路很好理解,用15位的二进制数来表示所有状态。
中间有一个存路径的操作,以后可以借鉴。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;

const int INF=1<<30;

struct Node
{
    char name[105];
    int dead,cost;
}cla[18];

struct DP
{
    int pre,time,spent,now;
}dp[1<<15];

int main()
{
    int t,i,j,s,n,end;
    //scanf("%d",&t);
    cin>>t;
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        //scanf("%d",&n);
        cin>>n;
        for(int i=0;i<n;i++)
            //scanf("%s%d%d",cla[i].name,&cla[i].dead,&cla[i].cost);
            cin>>cla[i].name>>cla[i].dead>>cla[i].cost;
        int en=1<<n;
        for(int s=1;s<en;s++)
        {
            dp[s].spent=INF;
            for(int i=n-1;i>=0;i--)
            {
                int tem=1<<i;
                if(s&tem)
                {
                    int past=s-tem;
                    int st=dp[past].time+cla[i].cost-cla[i].dead;
                    if(st<0)
                        st=0;
                    if(dp[s].spent>dp[past].spent+st)
                        {dp[s].spent=dp[past].spent+st;
                            dp[s].now=i;
                            dp[s].pre=past;
                            dp[s].time=dp[past].time+cla[i].cost;

                        }
                }
            }
        }
        stack<int> S;
        int tem = en-1;
        cout << dp[tem].spent << endl;
        while(tem)
        {
            S.push(dp[tem].now);
            tem = dp[tem].pre;
        }
        while(!S.empty())
        {
            cout << cla[S.top()].name << endl;
            S.pop();
        }
    }
    return 0;
}
View Code

 

posted on 2016-05-07 16:34  JASONlee3  阅读(168)  评论(0编辑  收藏  举报

导航