hud 1074 Doing Homework(经典dp)

来源:互联网 发布:dy530源码 编辑:程序博客网 时间:2024/05/26 02:53
<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
原创粉丝点击