Doing Homework_状态压缩&&位运算

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 34   Accepted Submission(s) : 22

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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,用二进制的数来表示做作业的状态,1表示做了,0表示没做;

dp[i]表示状态i损失的分数,i&x==0则表示作业x还没做,将要做x进入dp[j];

若要有dp[a],dp[b]均可以到达dp[j],那么选择损失分数最小的,若分数相同,按题意的字典序,即选择a和b中较小的一个,因为题目的课程是按字典序给出的。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=1<<16;
struct node
{
    int time,pre,sc;
} dp[N];
struct no
{
    int dt;
    int sc;
    char na[201];
} a[N];
int vis[N];

void print(int kfc)//递归输出
{
    int cur=dp[kfc].pre^kfc;//刚做的课程
    int cnt=0;
    cur>>=1;
    while(cur)
    {
        cnt++;
        cur>>=1;
    }
    if(dp[kfc].pre!=0)
        print(dp[kfc].pre);
    cout<<a[cnt].na<<endl;
}
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            cin>>a[i].na>>a[i].dt>>a[i].sc;
        }
        dp[0].time=0;dp[0].sc=0;dp[0].pre=-1;
        vis[0]=1;
        for(int i=0;i<(1<<n)-1;i++)//(1<<n)-1所有状态,都为1时,表示作业都做完了;遍历所有状态
        {
            for(int w=0;w<n;w++)
            {
                int cur=1<<w;
                if((cur&i)==0)//该课程还没做
                {
                    int k=cur|i;//下一步做得课程
                    dp[k].time=dp[i].time+a[w].sc;
                    int score=dp[k].time-a[w].dt;
                    if(score<0) score=0;
                    score+=dp[i].sc;
                    if(vis[k])//已经标记过,与已经标记的分数比较,选出小的
                    {
                        if(score<dp[k].sc)
                        {
                            dp[k].sc=score;
                            dp[k].pre=i;
                        }
                        else if(score==dp[k].sc)//若分数相等,则按前一状态小的记录
                        {
                            if(dp[k].pre>i)
                                dp[k].pre=i;
                        }
                    }
                    else
                    {
                        vis[k]=1;
                        dp[k].sc=score;
                        dp[k].pre=i;
                    }


                }

            }
        }
        cout<<dp[(1<<n)-1].sc<<endl;
        print((1<<n)-1);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/iwantstrong/p/5737172.html