hdu1074

简单的状态压缩,用二进制数i表示该状态下完成的课程,枚举该状态从某个状态转移过来,计算score


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct node
{
    char a[101];
    int dead,cost;
}str[16];
struct kk
{
    int pre,time,score;
}dp[(1<<15)+5];
int s[17];
int judge(int time,int num)
{
    if(time>=str[num].dead) return (time-str[num].dead+str[num].cost);
    if(time+str[num].cost<=str[num].dead) return 0;
    else return (time+str[num].cost-str[num].dead);
}
void dfs(int v)
{
    if(!v) return;
    dfs(v-s[dp[v].pre]);
    printf("%s\n",str[dp[v].pre].a);
}
int main()
{
    int i,j,t,n,k;
    for(s[1]=1,i=2;i<17;i++) s[i]=s[i-1]<<1;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%s %d %d",str[i].a,&str[i].dead,&str[i].cost);
        for(i=1;i<(1<<n);i++)
        {
            for(j=n;j>=1;j--)
                if(i&s[j])
                {
                    if((!dp[i].time)||dp[i-s[j]].score+judge(dp[i-s[j]].time,j)<dp[i].score)
                    {
                        dp[i].pre=j;
                        dp[i].time=dp[i-s[j]].time+str[j].cost;
                        dp[i].score=dp[i-s[j]].score+judge(dp[i-s[j]].time,j);
                    }
                }
        }
        k=(1<<n)-1;
        printf("%d\n",dp[k].score);
        dfs(k);
    }
}


你可能感兴趣的:(hdu1074)