hdu 【1074】Doing Homework


Doing Homework

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


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.
/* 首先大家要理解什么是位运算,1<<n=2^n;相当于在1的右边加n个0,这道题我还是利用递推,但是
状态不是很好想,要利用2进制对状态进行表示。n个作业全部做完,相当于2进制表示的n个1,对于
每个作业是是否已经做了,我们只需要利用位运算i&&(1<<j)是否为0即可求出来。这里共有1<<n个状态;
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

struct course
{
    char name[105];
    int dead,cost;
}a[20];

struct Node
{
    int cost;
    int pre;
    int reduced;
}dp[1<<16];
bool vis[1<<16];       //判断是否这个状态是否走过

int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
            scanf("%s%d%d",a[i].name, &a[i].dead, &a[i].cost);
        memset(vis,false,sizeof(vis));
        dp[0].cost = 0;
        dp[0].pre = -1;
        dp[0].reduced = 0;
        vis[0] = true;
        int upper = 1 << n;
        for(int i = 0; i < upper; i++)
        {
            for(int j = 0; j < n; j++)
            {
                int bin = 1<<j;
                if((i&bin) == 0)
                {
                    int temp = i|bin;
                    int cost = dp[i].cost + a[j].cost ;
                    int reduce = cost - a[j].dead;
                    if(reduce < 0) reduce = 0;     //有可能有负数
                    reduce += dp[i].reduced;
                    if(vis[temp] && reduce < dp[temp].reduced)
                    {
                        dp[temp].cost = cost;
                        dp[temp].pre = j;
                        dp[temp].reduced = reduce;
                    }
                    else if(!vis[temp])
                    {
                        vis[temp] = true;
                        dp[temp].cost = cost;
                        dp[temp].pre = j;
                        dp[temp].reduced = reduce;
                    }
                }
            }
        }
        printf("%d\n",dp[--upper].reduced);
        stack <int> S;
        int temp = dp[upper].pre;
        while(temp != -1)
        {
            S.push(temp);
            upper -= 1 << dp[upper].pre;  //这里我直接减去,相当于去掉特定位置的1
            temp = dp[upper].pre;
        }
        while(!S.empty())
        {
            printf("%s\n",a[S.top()].name);
            S.pop();
        }
    }
    return 0;
}


你可能感兴趣的:(hdu 【1074】Doing Homework)