hdu 1074 Doing Homework 状压dp

题目: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;
}

转载于:https://www.cnblogs.com/01world/p/5824084.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值