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门作业,每个作业有一个截止时间和完成该作业需要的天数,超出截止时间完成要受到惩罚,每超过一天惩罚加一,问完成所有作业最小的惩罚,并且输出做作业的顺序,若有多个解,输出作业最小字典序。

题解:因为n<=15所以该题可以用状压dp,dp[sta]表示:状态为sta的情况下最小的惩罚(10分别表示做和没做),我们还需要一个time[sta]数组表示:状态为sta的情况下需要的时间,再用一个pre[]数组记录一下做题顺序。

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF  =  0x3f3f3f3f;
const int O    =  1e6;
const int mod  =  10007;
const int maxn =  1e2+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

const int _ = 1;
const int __ = 2;
const int ___ = 3;

struct dd{
    string name;
    int end, need;
    bool friend operator < (dd a, dd b){
        return a.name > b.name;
    }
}a[30];

int pre[1<<15], dp[1<<15], Time[1<<15];

void print(int sta){
    if(!sta) return;
    print(sta ^ (1 << pre[sta]));
    cout<< a[pre[sta]].name <<endl;
}

int main(){
    int T; scanf("%d", &T);
    while( T -- ){
        
        int n; scanf("%d", &n);
        for(int i=0; i<n; i++) cin>> a[i].name >> a[i].end >> a[i].need;
        sort(a, a+n);
        
        for(int i=0; i<1<<n; i++) {
            dp[i] = i == 0 ? 0 : INF;
            pre[i] = -1;
        }
        
        for(int sta=1; sta<1<<n; sta ++) {
            for(int i=0; i<n; i++) {
                if(sta & (1<<i)) {
                    int t = Time[sta ^ (1<<i)] + a[i].need - a[i].end;
                    t = t > 0 ? t : 0;        // t 表示第i个作业所产生的惩罚
                    if(t + dp[sta ^ (1<<i)] < dp[sta]) {
                        dp[sta]  = t + dp[sta ^ (1<<i)];
                        pre[sta] = i;
                        Time[sta] = a[i].need + Time[sta ^ (1<<i)];
                    }
                }
            }
        }
        
        printf("%d\n" , dp[(1<<n) -1]);
        print((1<<n) - 1);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值