HDU 1074 Doing Homework (状压DP)

Doing Homework

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

 

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

Subject to the effect

Will not finish the job, each job (in lexicographic order has good discharge) time and have a limited time, after a defined time post operation, one mark per day, and now points corresponding minimum work sequence (when the same minimum value, a case lexicographically smallest output)

Topic analysis

Note that only a maximum of 15 jobs, so you can use a binary number to indicate the completion of the current situation: for example, 111 is all completed, the 101 is the second did not finish

If there are seven jobs, it is binary 1111111 i.e. 2 ^ 7-1 we enumerate 127 i.e. from 1 to 127 with dp [i] to represent the current state of the buckle reaches a minimum score

J enumerate from back to front and then a single job (Why As from back to front after say), this job is not to determine the current state has been completed, such as 101, is the first job and the third job is completed a, then the current state can be from a previous state converted into one, such as 101 can be converted into one from the 100 or 001 so that it can be updated, it is apparent dp [i] = max (dp [i], dp [previous status] + a [j])

We can now explain why traverse a single job from back to front, because the transfer equation based on the above, we can see that the current state is reached after the completion of this work, that is, the job is finally done, and title requirements If you experience the same minimum time, in order to plan the minimum output dictionary order, it must traverse forward from the rear

Of course, to do the output of the order of questions, you should always record what the current state is a state, and what the current completion of the work.

Code:

#include <bits/stdc++.h>  

using namespace std; 

typedef struct 
{
    string name;
    int dday;
    int cost;
}homework;

struct 
{
    int pre;
    int name;
    int vul;
    int time;
}dp[1<<15];

homework a[105];
int tot,t,n,i,j;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        memset(dp,0,sizeof(dp));
        for(i=0;i<n;i++)
        {
            cin>>a[i].name>>a[i].dday>>a[i].cost;
        }
        tot=1<<n;
        for(i=1;i<tot;i++)
        {
            dp[i].vul=0x7fffff;
            for(j=n-1;j>=0;j--)
            {
                int temp=1<<j;
                if(temp&i)
                {
                    int s=dp[i-temp].time+a[j].cost-a[j].dday;
                    if(s<0)
                    s=0;
                    if(dp[i].vul>dp[i-temp].vul+s)
                    {
                        dp[i].pre=i-temp;
                        dp[i].name=j;
                        dp[i].vul=dp[i-temp].vul+s;
                        dp[i].time=dp[i-temp].time+a[j].cost;
                    }
                }
            }
        }
        cout<<dp[tot-1].vul<<endl;
        i=tot-1;
        stack<int>q;
        while(dp[i].time)
        {
            q.push(dp[i].name);
            i=dp[i].pre;
        }
        while(!q.empty())
        {
            cout<<a[q.top()].name<<endl;
            q.pop();
        }
    }
}

 

 

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/11355657.html