HDU1074 Doing Homework 状态压缩DP

Doing Homework

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

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

题解

咋一看题目觉得像贪心,其实是DP题,而且是需要状态压缩的DP,因为最多有15个科目的作业,如果先完成1,2,3,和先完成3,2,1他们所需要的时间是一样的,只是惩罚不同,这样的话就有15!种情况,太大了。又因为只有15个科目的作业,故可以用二进制2^15来表示,dp[i]表示做完i的最少惩罚,那么什么时候能转移到dp[i]呢,假如对于作业k,i中含有作业k已完成,那么i可以由和i状态相同的状态仅仅是k未完成的状态来推导,用位运算 状态j=i-(1<< k),在这之前先用i&temp来判断有没有做完k,如果没有就直接跳过了。
其实有点像01背包,就像是一个有n个物品的背包,每个物品的体积是1,总体积为n,要求恰好装满这个背包的最小惩罚

代码

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define MAX 100000
const int maxn = 1<<15+10; 
#define LL long long
int cas=1,T;
struct homework
{
    string name;
    int needtime;          //完成需要的时间
    int deadline;          //期限
}hw[20];

struct DP
{
    int score;     //完成到状态i所需要的最少的惩罚
    int time;      //完成到状态i的时间
    int pre;       //记录路径
}dp[maxn];

void print(int x)
{
    if (!x) return;
    print(x-(1<<dp[x].pre));
    cout << hw[dp[x].pre].name << endl;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for (int i = 0;i<n;i++)
            cin >> hw[i].name >> hw[i].needtime >> hw[i].deadline;
        int bit = 1 << n;                                        //所有状态
        for (int i = 1;i<bit;i++)                              //枚举到达的状态i
        {
            dp[i].score = 99999999;
            for (int j = n-1;j>=0;j--)//逆序为了保证没后效性,想想01背包的例子?
            {               
                int temp = 1 << j;    
                if (temp & i)                                  //判断j是否做完
                {
                    int ttime = dp[i-temp].time + hw[j].deadline-hw[j].needtime;
                    if (ttime < 0) ttime = 0;
                    if (ttime + dp[i-temp].score < dp[i].score)
                    {
                        dp[i].score = ttime + dp[i-temp].score;
                        dp[i].time = dp[i-temp].time + hw[j].deadline;
                        dp[i].pre = j;
                    }
                }
            }
        }
        printf("%d\n",dp[bit-1].score);
        print(bit-1);
    }
    //freopen("in","r",stdin);
    //scanf("%d",&T);
    //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
    return 0;
}

你可能感兴趣的:(HDU1074 Doing Homework 状态压缩DP)