HDU 1074 Doing Homework「状压dp」

来源:互联网 发布:淘宝代购点什么意思 编辑:程序博客网 时间:2024/06/05 20:25

A - Doing Homework

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      

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.

题意:给出一些要做的作业最多15个,每个作业有截至时间和需要做的天数,超期一天扣一分,求让扣分最小的安排方案。

状态转移方程:dp[st | (1<< i)] = min( dp[st | ( 1 << i ) ] , dp[ st ] + 当前 i 超期的扣分 )

代码如下:

#include <algorithm>#include <bitset>#include <cassert>#include <climits>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <deque>#include <iomanip>#include <iostream>#include <map>#include <numeric>#include <queue>#include <set>#include <stack>#include <string>#define INF 0x3f3f3f3fusing namespace std;typedef long long ll;#define N 16int dp[1<<N];struct work{    string name;    int deadline,cost;};work a[N];int pre[1<<N];int n;void Output(int status){    if(status==0) return;    int t=0;    for(int i=0;i<n;i++){        if( (status&(1<<i))!=0 && (pre[status]&(1<<i))==0 ){            t=i;break;        }    }    Output(pre[status]);    cout<<a[t].name<<endl;}int main(){    int T;    scanf("%d",&T);    while(T--){        memset(dp,INF,sizeof(dp));        memset(pre,0,sizeof(pre));        scanf("%d",&n);        for(int i=0;i<n;i++){            cin>>a[i].name>>a[i].deadline>>a[i].cost;        }        dp[0]=0;        for(int st=0;st<(1<<n);st++){//            int temp=0;            for(int i=0;i<n;i++){                if(st&(1<<i)){                    temp+=a[i].cost;                }            }            for(int i=0;i<n;i++){                if((st&(1<<i))==0){                    if(dp[st|(1<<i)]>dp[st]+max(0,temp+a[i].cost-a[i].deadline)){                        dp[st|(1<<i)]=dp[st]+max(0,temp+a[i].cost-a[i].deadline);                        pre[st|(1<<i)]=st;                    }                }            }        }        printf("%d\n",dp[(1<<n)-1]);        Output((1<<n)-1);    }    return 0;}

不打铁,不打铁!

0 0
原创粉丝点击