hud 1074 Doing Homework(经典dp)

<span style="font-family:Microsoft YaHei;font-size:18px;">最近在刷简单dp,弱菜一枚~
对于dp的状态方程还是不能很好的理解。
多做题吧</span>

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

const int maxn = (1<<15)+10;
const int inf = 0x3f3f3f3f;
int dp[maxn],times[maxn],path[maxn];
struct ss {
    char name[100];
    int deadline,time;
} a[20];

int Max(int x,int y) {
    if(x > y) 
        return x;
    else
        return y;
}

void output(int x) {
    if(!x) return ;
    output(x - (1<<path[x]));
    printf("%s\n",a[path[x]].name);
}
int main() {
    int t,n;
    scanf("%d",&t);
    while(t--) {
        scanf("%d",&n);
        for(int i = 0;i < n;i++) 
            scanf("%s%d%d",a[i].name,&a[i].deadline,&a[i].time);
        memset(times,0,sizeof(times));
        //memset(dp,0,sizeof(dp));
        for(int i = 1;i < (1<<n);i++) {//递推每个状态
            dp[i] = inf;
            for(int j = n-1;j>=0;j--) {//比较每门课放在最后面交的结果,将最优结果存入dp[i]
                int temp = 1<<j;
                if(!(i&temp)) continue;
                int cc = times[i-temp] + a[j].time - a[j].deadline;
                cc = Max(cc,0);
                if(dp[i-temp] + cc < dp[i]) {
                    dp[i] = dp[i-temp] + cc;
                    times[i] = times[i-temp] + a[j].time;//更新最优解时的时间
                    path[i] = j;//更新最优解时的课程顺序
                }
            }
        }
        printf("%d\n",dp[(1<<n)-1]);
        output((1<<n)-1);
    }
}


 
 
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值