Doing Homework

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.

代码

#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<climits>
#include<queue>
#include<set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define swap(x,y) (x^=y,y^=x,x^=y)
const int N=20;
typedef long long ll;
const int INF=0x3f3f3f3f;
using namespace std;
template<typename T>inline void read(T &x)
{
    x=0;
    T f=1;
    char c=getchar();
    for(; c<'0'||c>'9'; c=getchar()) if(c=='-') f=-1;
    for(; c>='0'&&c<='9'; c=getchar()) x=(x<<1)+(x<<3)+(c&15);
    x*=f;
}
template<typename T>inline void print(T x)
{
    if(x<0) putchar('-'),x*=-1;
    if(x>=10) print(x/10);
    putchar(x%10+'0');
}
struct node
{
    string s;
    int d,c;
} a[N];
int dp[1<<N];
int pt[1<<N];
int path[1<<N];
void COUT(int x)
{
    if(!x) return ;
    COUT(x-(1<<path[x]));
    cout<<a[path[x]].s<<endl;
}
int main()
{
    int t,n;
    read(t);
    string s;
    int l,r;
    while(t--)
    {
        memset(pt,0,sizeof(pt));
        read(n);
        for(int i=0; i<n; i++)
        {
            cin>>s>>l>>r;
            a[i].s=s;
            a[i].d=l;
            a[i].c=r;
        }
//        memset(dp,INF,sizeof(dp));
        int pre,cur;
        for(int i=1; i<(1<<n); i++)
        {
            dp[i]=INF;
            for(int j=n-1; j>=0; j--)   //因为题目中说按科目递增顺序输入
            {                              // 所以倒序循环遍历,保证字典序。
                cur=(1<<j);              //如果字典序跟非字典序dp值相同,则保证了字典序也被遍历到。
                if((cur&i)==0)
                    continue;
                pre=i-cur;
                int sc=a[j].d-a[j].c-pt[pre];
                if(sc>0)  //完成 <0未完成 扣分
                    sc=0;
                if(dp[i]>dp[pre]-sc)    
                {
                    dp[i]=dp[pre]-sc;
                    pt[i]=pt[pre]+a[j].c;
                    path[i]=j;
                }
            }
        }
        print(dp[(1<<n)-1]);
        puts("");
        COUT((1<<n)-1);
    }
    return 0;
}

思路

有n本书就有2^n 次方种状态,从1开始遍历每种状态,得到每一步的最优解。

ps

状压dp ,没思路,看的题解。减少开结构体,用数组代替较好。 记录路径并且回溯。

posted @ 2019-11-09 20:25  Anticlock  阅读(206)  评论(0编辑  收藏  举报