DP专题-----D - Doing Homework (状压DP)

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.

因为n是15,可以想一下状态压缩,来表示所有情况,转台转移为:
dp[i | (1 << k)] = min(dp[i | (1 << k)],dp[i] + tmp);
i状态转化为i | (1 << k)是i表示的二进制位,第k位还没选,那么 下一个就是选k,先算一下,i状态一共花费的时间,之后再算如果加上第k项,会减少的分数是多少,这个就是tmp;
具体详见代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef struct Node{
    string s;
    int deadline;
    int stay;
}Node;
Node node[16];

int dp[40005];
int pre[40005];
int n;

void dfs(int x)
{
    if(x == 0) return ;
    int t = 0;
    for(int i = 0;i < n;++i){
        if(((x & (1 << i)) != 0) && ((pre[x] & (1 << i)) == 0)){
            t = i;
            break;
        }
    }
    dfs(pre[x]);
    printf("%s\n",node[t].s.c_str());
}

int main()
{
    int t;
    while(~scanf("%d",&t)){
        while(t--){
            scanf("%d",&n);
            for(int i = 0;i < n;++i){
                char s[105];
                int x,y;
                scanf("%s %d %d",s,&x,&y);
                node[i].s = s;
                node[i].deadline = x;
                node[i].stay = y;
                //printf("%s %d %d\n",node[i].s.c_str(),node[i].deadline,node[i].stay);
            }
            for(int i = 0;i < (1 << n);++i)
                dp[i] = inf;
            dp[0] = 0;
            for(int i = 0;i < (1 << n);++i){
                for(int j = 0;j < n;++j){
                    if((i & (1 << j)) != 0) continue;
                    //算出当前状态的所减分数值
                    int tmp = 0;
                    for(int k = 0;k < n;++k){
                        if((i & (1 << k)) != 0){
                            tmp += node[k].stay;
                        }
                    }
                   // cout << i << " " << tmp << endl;
                    tmp += node[j].stay;
                    if(tmp > node[j].deadline){
                        tmp = tmp - node[j].deadline;
                    }else{
                        tmp = 0;
                    }
                    if(dp[i | (1 << j)] > dp[i] + tmp){
                        dp[i | (1 << j)] = dp[i] + tmp;
                        //cout << (i | (1 << j)) << " " << i << " " <<tmp << endl;
                        pre[i | (1 << j)] = i;
                    }
                }
            }
            printf("%d\n",dp[((1 << n) - 1)]);
            dfs((1 << n) - 1);
        }
    }
    return 0;
}

发布了269 篇原创文章 · 获赞 33 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/88949781