状态压缩DP

来源:互联网 发布:笔记本win10优化教程 编辑:程序博客网 时间:2024/06/01 19:21

Doing Homework

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 10

Font: Times New Roman | Verdana | Georgia

Font Size:

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

23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3

Sample Output

2ComputerMathEnglish3ComputerEnglishMath代码: #include"stdio.h"#include"string.h"#define Max 1<<16struct node{    int cost;    int pre;    int reduced;}dp[Max];struct Cou{    int deadt;    int cost;    char name[110];}cou[16];int vis[Max];void output(int k){    int curj=dp[k].pre^k;    int curi=0;    curj>>=1;    while(curj)    {        curi++;        curj>>=1;    }    if(dp[k].pre)        output(dp[k].pre);    printf("%s\n",cou[curi].name);}int main(){    int t,n,i,j;    int u,day,cur,temp,re;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        u=(1<<n)-1;        for(i=0;i<n;i++)                scanf("%s %d%d",cou[i].name,&cou[i].deadt,&cou[i].cost);        memset(vis,0,sizeof(vis));        dp[0].cost=0;        dp[0].pre=-1;        dp[0].reduced=0;        vis[0]=1;        for(i=0;i<u;i++)        {            for(j=0;j<n;j++)            {                cur=1<<j;                if((cur&i)==0)                {                    temp=cur|i;                    day=dp[i].cost+cou[j].cost;                    dp[temp].cost=day;                    re=day-cou[j].deadt;                    if(re<0)re=0;                    re+=dp[i].reduced;                    if(vis[temp])                    {                        if(re<dp[temp].reduced)                        {                            dp[temp].reduced=re;                            dp[temp].pre=i;                        }                    }                    else                    {                        vis[temp]=1;                        dp[temp].reduced=re;                        dp[temp].pre=i;                    }                }            }        }        printf("%d\n",dp[u].reduced);        output(u);    }    return 0;}
0 0
原创粉丝点击