hdu 1074 Doing Homework 状压dp

来源:互联网 发布:java中时间格式 编辑:程序博客网 时间:2024/05/17 06:05

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1074

题意:

有n门课程作业,每门作业的截止时间为D,需要花费的时间为C,若作业不能按时完成,每超期1天扣1分。
这n门作业按课程的字典序先后输入
问完成这n门作业至少要扣多少分,并输出扣分最少的做作业顺序
PS:达到扣分最少的方案有多种,请输出字典序最小的那一组方案

分析:

n<=15,只需对这n份作业进行全排列,选出扣分最少的即可。显然是用状压dp来解决。

const int INF=0x7fffffff;const int N=(1<<15)+9;int f[20],d[20];int dp[N],day[N],pre[N]; //最少扣分,用的天数,记录路径char s[20][111];int n;void print_ans(int x){    if(!x)return;    print_ans(x-(1<<pre[x]));    printf("%s\n",s[pre[x]]);}int main(){    int T; scanf("%d",&T);    while(T--){        scanf("%d",&n);        for(int i=0;i<n;i++)scanf("%s%d%d",&s[i],&d[i],&f[i]);        dp[0]=0;        for(int st=1;st<(1<<n);st++){            dp[st]=INF;            for(int i=0;i<n;i++){                int t=1<<i;                if(!(st&t))continue;                int score=day[st-t]+f[i]-d[i];                if(score<0)score=0;                if(dp[st]>=dp[st-t]+score){ //i要尽量靠后,因为i是从后往前完成的,所以是>=                    dp[st]=dp[st-t]+score;                    day[st]=day[st-t]+f[i];                    pre[st]=i;                }            }        }        printf("%d\n",dp[(1<<n)-1]);        print_ans((1<<n)-1);    }    return 0;}
0 0
原创粉丝点击